-
Notifications
You must be signed in to change notification settings - Fork 0
/
masker.js
267 lines (261 loc) · 11 KB
/
masker.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*!
* MaskerJS v1.22 - Vanilla Javascript mask plugin to input form elements
* Copyright 2019-2024 Silvio Delgado (https://github.com/silviodelgado)
* Licensed under MIT (https://opensource.org/licenses/MIT)
* https://github.com/silviodelgado/maskerjs
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory(root));
} else if (typeof exports === 'object') {
module.exports = factory(root);
} else {
root.Masker = factory(root);
}
})(typeof global !== "undefined" ? global : this.window || this.global, function (root) {
'use strict';
String.prototype.replaceAll = function (find, replace) {
let str = this;
while (str.indexOf(find) >= 0) {
str = str.replace(find, replace);
}
return str;
};
const masks = {
cpf: function (target) {
return target.value
.replace(/\D/g, '')
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{3})(\d{1,2})/, '$1-$2')
.replace(/(-\d{2})\d+?$/, '$1');
},
cnpj: function (target) {
return target.value
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '$1.$2')
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{3})(\d{1,4})/, '$1/$2')
.replace(/(\/\d{4})(\d{1,2})/, '$1-$2')
.replace(/(-\d{2})\d+?$/, '$1');
},
cpf_cnpj: function (target) {
let paste = target.value == target.value.replace(/\D/g, '');
if ((!paste && target.value.length <= 14) || (paste && target.value.length == 11)) {
return masks.cpf(target);
}
return masks.cnpj(target);
},
cep: function (target) {
return target.value
.replace(/\D/g, '')
.replace(/(\d{5})(\d)/, '$1-$2')
.replace(/(-\d{3})\d+?$/, '$1');
},
phone: function (target) {
return target.value.replace(/\D/g, '')
.replace(/(\d{1,2})/, '($1')
.replace(/\((\d{2})(\d{1})/, '($1) $2')
.replace(/(\d{4})(\d{1,4})/, '$1-$2')
.replace(/(\d{4})-(\d)(\d{4})/, '$1$2-$3')
.replace(/(-\d{4})\d+?$/, '$1');
},
date: function (target) {
return target.value
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '$1/$2')
.replace(/(\d{2})(\d{1,4})/, '$1/$2')
.replace(/(\/\d{4})\d+?$/, '$1');
},
datetime: function (target) {
return target.value
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '$1/$2')
.replace(/(\d{2})(\d{1,4})/, '$1/$2')
.replace(/(\d{4})(\d{1,2})/, '$1 $2')
.replace(/(\d{4}) (\d{2})(\d{1,2})/, '$1 $2:$3')
.replace(/(\:\d{2})\d+?$/, '$1');
},
year_month: function (target) {
if (target.value.length > 7) return target.value.substring(0, 7);
return target.value
.replace(/\D/g, '')
.replace(/(\d{4})(\d)/, '$1/$2')
.replace(/(\/\d{2})\d+?$/, '$1');
},
month_year: function (target) {
if (target.value.length > 7) return target.value.substring(0, 7);
return target.value
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '$1/$2')
.replace(/\/(\d{4})\d+?$/, '$1');
},
time: function (target) {
return target.value
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '$1:$2')
.replace(/(\:\d{2})\d+?$/, '$1');
},
money: function (target) {
let culture = target.dataset.culture || 'en-us';
let firstTime = !(target.dataset.masked || false);
target.setAttribute('maxlength', 18);
let value = target.value;
while (value.length > 0 && (value.substring(0, 1) == '0' || value.substring(0, 1) == '.' || value.substring(0, 1) == ',')) {
value = value.substring(1);
}
if (value.length == 1) {
value = '0.0' + value;
} else if (value.length == 2) {
value = '0.' + value;
}
switch (culture.toLowerCase()) {
case 'pt-br':
target.setAttribute('placeholder', '0,00');
value = (firstTime
? parseFloat(value).toFixed(2).toString()
: value.replaceAll('.', '').replace(',', '.'))
.replace(/\D/g, '')
.replace(/(\d{1, 2})$/, '$1')
.replace(/(\d+)(\d{2})/, '$1,$2')
.replace(/(\d+)(\d{3})/, '$1.$2')
.replace(/(\d+)(\d{3})/, '$1.$2')
.replace(/(\d+)(\d{3})/, '$1.$2')
.replace(/(\d+)(\d{3})/, '$1.$2');
break;
default:
target.setAttribute('placeholder', '0.00');
value = (firstTime
? parseFloat(value).toFixed(2).toString()
: value.replace(',', '.'))
.replace(/\D/g, '')
.replace(/(\d{1, 2})$/, '$1')
.replace(/(\d+)(\d{2})/, '$1.$2')
.replace(/(\d+)(\d{3})/, '$1,$2')
.replace(/(\d+)(\d{3})/, '$1,$2')
.replace(/(\d+)(\d{3})/, '$1,$2')
.replace(/(\d+)(\d{3})/, '$1,$2');
break;
}
target.dataset.masked = true;
return value;
},
ccard: function (target) {
let result = target.value.replace(/\D/g, '');
let first = result.length > 0 ? result.substring(0, 1) : '';
if (first == '') return '';
switch (first) {
case '4':
case '5':
case '6':
target.setAttribute('maxlength', 19);
return result
.replace(/(\d{4})(\d)/, '$1 $2')
.replace(/( \d{4})(\d)/, '$1 $2')
.replace(/( \d{4})(\d)/, '$1 $2')
.replace(/( \d{4})(\d+?$)/, '$1 $2');
case '3':
let second = result.length > 1 ? result.substring(1, 1) : '';
if (second == '') return result;
switch (second) {
case '6':
case '8':
target.setAttribute('maxlength', 16);
return result
.replace(/(\d{4})(\d)/, '$1 $2')
.replace(/( \d{6})(\d)/, '$1 $2')
.replace(/( \d{4})(\d+?$)/, '$1 $2');
case '4':
case '7':
target.setAttribute('maxlength', 17);
return result
.replace(/(\d{4})(\d)/, '$1 $2')
.replace(/( \d{6})(\d)/, '$1 $2')
.replace(/( \d{5})(\d+?$)/, '$1 $2');
default:
return result;
}
default:
return result;
}
},
number: function (target) {
return target.value
.replace(/\D/g, '');
},
float: function (target) {
return target.value
.replace(/[^\-0-9.,]/g, '')
},
percent: function (target) {
let culture = target.dataset.culture || 'en-us';
let precision = parseInt(target.dataset.precision || '2');
let firstTime = !(target.dataset.masked || false);
let maxValue = parseFloat(target.dataset.max || '100.00');
target.setAttribute('maxlength', (4 + precision));
let value = target.value;
while (value.length > 0 && (value.substring(0, 1) == '0' || value.substring(0, 1) == '.' || value.substring(0, 1) == ',')) {
value = value.substring(1);
}
while (value.length > 0 && value.length <= precision) {
value = '0.' + value.padStart(precision, '0');
}
let regex1 = new RegExp('(\\d{1,' + precision + '})$');
let regex2 = new RegExp('(\\d{1,3})(\\d{' + precision + '})');
switch (culture.toLowerCase()) {
case 'pt-br':
target.setAttribute('placeholder', '0,' + '0'.padEnd(precision, '0'));
value = (firstTime
? parseFloat(value).toFixed(precision).toString()
: value.replace(',', '.'))
.replace(/\D/g, '')
.replace(regex1, '$1')
.replace(regex2, '$1,$2');
break;
default:
target.setAttribute('placeholder', '0.' + '0'.padEnd(precision, '0'));
value = (firstTime
? parseFloat(value).toFixed(precision).toString()
: value)
.replace(/\D/g, '')
.replace(regex1, '$1')
.replace(regex2, '$1.$2');
break;
}
target.dataset.masked = true;
let val = parseFloat(value.replace(/\D/g, '')
.replace(regex1, '$1')
.replace(regex2, '$1.$2'));
if (val > maxValue) {
value = value.substring(0, value.length - 1);
}
return value;
}
};
const init_component = function () {
document.querySelectorAll('input[data-mask]').forEach(($elem) => {
if (!$elem.dataset.mask)
return false;
$elem.value = masks[$elem.dataset.mask] ? masks[$elem.dataset.mask]($elem) : $elem.value;
$elem.addEventListener('input', (e) => {
if (e.data == null && e.target.value != '') {
e.target.value = e.target.value.substring(0, e.target.value.length);
return;
}
e.target.value = masks[$elem.dataset.mask](e.target);
});
$elem.addEventListener('blur', (e) => {
e.target.value = masks[$elem.dataset.mask](e.target);
});
$elem.addEventListener('keyup', (e) => {
e.target.value = masks[$elem.dataset.mask](e.target);
});
$elem.addEventListener('focus', (e) => {
$elem.select();
});
});
};
return {
init: init_component
};
});