-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask-amd.js
156 lines (137 loc) · 3.67 KB
/
mask-amd.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
define('mask', ['doc'], function($) {
var charToRegex = function(patternLetter) {
switch(patternLetter) {
case "9":
return new RegExp("[0-9]");
case "A":
return new RegExp("[A-Za-z]");
case "":
return "end";
default:
return "literal";
}
}
function format(value, pattern) {
value = value.replace(/\D/g, '').replace(/^0+/, '');
var i = value.length - 1,
j = pattern.length - 1,
formatted = '';
while (j >= 0) {
if (pattern[j] === '.') {
formatted = '.' + formatted;
j--;
}else if (pattern[j] === ',') {
formatted = ',' + formatted;
j--;
}
if (i >= 0) {
formatted = value[i] + formatted;
} else if (pattern[j] === '0'){
formatted = '0' + formatted;
}
i--; j--;
}
return formatted;
};
var applyNumberMask = function(el) {
var pattern = el.getAttribute('mask-number'),
$el = $(el),
oldVal = $el.val();
$el.on('input', function(event) {
var inputContent = $el.val();
if (oldVal.indexOf(inputContent) === -1 || oldVal == inputContent) {
$el.val(format(inputContent, pattern));
}
oldVal = $el.val();
});
$el.on('blur', function(event) {
var inputContent = $el.val();
if (inputContent.trim() === '') {
return;
}
var newVal = format(inputContent, pattern);
$el.val(newVal);
oldVal = newVal;
});
};
var applyMask = function(el) {
var pattern = el.getAttribute("mask"),
_currentValue = el.value;
var addletter = function(input, key, e) {
var patternLetter = pattern.charAt(input.value.length - 1),
regexResult = charToRegex(patternLetter);
if(regexResult === "literal") {
el.value = el.value.slice(0, el.value.length - 1);
input.value = input.value + patternLetter + key;
addletter(input, key, e);
return;
}
if(regexResult === "end") {
e.preventDefault();
return;
}
if(!key.match(regexResult)) {
el.value = el.value.slice(0, el.value.length - 1);
return;
}
};
var eventKey = 'input';
$(el).on(eventKey, function(e) {
if(_currentValue.length == el.value.length) {
e.stopPropagation();
_currentValue = el.value;
return;
} else if (pattern.length < el.value.length) {
e.stopPropagation();
el.value = el.value.slice(0, el.value.length - 1);
return;
}
var letter = el.value.slice(-1);
addletter(el, letter, e);
setTimeout(function() {
if(el.createTextRange) {
var textRange = el.createTextRange();
textRange.collapse(true);
textRange.moveStart('character', el.value.length + 1);
textRange.moveEnd('character', 0);
textRange.select();
} else if (el.setSelectionRange) {
el.setSelectionRange(el.value.length + 1, el.value.length + 1);
el.value = el.value;
}
_currentValue = el.value;
}, 0);
_currentValue = el.value;
});
(function format(value) {
if(!value) {
return;
}
var formattedValue = "", maskIndex = 0;
var formatPattern = el.getAttribute("mask-pattern"),
value = value.replace(formatPattern, '');
for(var valueIndex = 0; valueIndex < value.length; valueIndex++) {
var patternLetter = pattern.charAt(maskIndex++),
regexResult = charToRegex(patternLetter);
if(regexResult === "literal") {
formattedValue += patternLetter;
maskIndex++;
} else if (regexResult === "end") {
break;
}
if(regexResult === "literal" && patternLetter === value.charAt(valueIndex)) {
maskIndex--;
continue;
}
formattedValue += value.charAt(valueIndex);
}
$(el).val(formattedValue);
})($(el).val());
}
$('[mask]').each(applyMask);
$('[mask-number]').each(applyNumberMask);
return {
'applyMask' : applyMask,
'applyNumberMask' : applyNumberMask
}
});