-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartedInput.jquery.js
84 lines (71 loc) · 3.03 KB
/
partedInput.jquery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(function ($) {
$.fn.partionedInput = function (options) {
// Establish our default settings
var settings = $.extend({
type: 'text',
size: 4,
styleClass: 'parted-input',
styleIdPrefix: 'input-part-',
regex: null,
}, options);
var defaultTextRegex = /[0-9]|[a-z]|[A-Z]/;
var defaultNumberRegex = /[0-9]/;
var jqueryElem = null;
var regex = settings.regex || (settings.type.toUpperCase() === "TEXT" ?
defaultTextRegex : defaultNumberRegex);
$.fn.keydownFun = function (event) {
if (!event || !event.key) {
return false;
}
event.preventDefault();
var isFirst = jqueryElem[0] === this[0];
var isLast = jqueryElem[settings.size - 1] === this[0];
switch (event.key.toUpperCase()) {
case "BACKSPACE":
case "DELETE":
$(this).val("");
!isFirst && $(this)[0].previousElementSibling.focus();
break;
case "TAB":
isLast ? $(this).blur() : $(this)[0].nextElementSibling.focus();
break;
default:
// In case of keys like Control, Alt, F1 and etc regex will pass in case of text input.
// Using length of the input to determine if it is a character or other keys
event.key.length === 1 && regex.test(event.key) && ($(this).val(event.key) &&
isLast && $(this).blur() || $(this)[0].nextElementSibling.focus());
break;
}
return false;
}
// This function will only work in those browser in which onpaste event is supported
$.fn.pasteFun = function (event) {
event.preventDefault();
if(!event || !event.clipboardData) {
return false;
}
var text = event.clipboardData.getData("text") || "";
jqueryElem.each(function(num, el) {
$(el).val(text[num]);
});
return false;
}
var htmlStr = "<input type='" + settings.type + "' class='" +
settings.styleClass + "' id='" + settings.styleIdPrefix +
"%' onkeyup='$(this).keydownFun(event)' onpaste='$(this).pasteFun(event)' />";
var str = "";
for (var i = 0; i < settings.size; i++)
str += htmlStr.replace("%", i);
$(this).replaceWith(jqueryElem = $(str));
jqueryElem.value = function (val) {
val = typeof val === "string" || Array.isArray(val) ? val : typeof val === "number" ? "" + val : [];
var str = "";
jqueryElem.each(function (num, elem) {
val[num] && $(elem).val(val[num]);
str += $(elem).val();
});
return str;
};
return jqueryElem;
}
}(jQuery));