-
Notifications
You must be signed in to change notification settings - Fork 1
/
mask.js
56 lines (52 loc) · 1.65 KB
/
mask.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
/**
* Script para adicionar mascará nos formulários
* Para utilizar basta adicionar na tag html o attr
* data-input="NOME do Controle".
* Autor: Bruno Salmito Filizola
* Versão: 0.1
*/
//Objeto mask para adicionar maskara aos campos do formulário
const mask = {
cpf(value) {
return value
.replace(/\D/g, '')
//D somente números
//d somente letras
//g global
.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(value) {
return value
//nao aceita nada além de número
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '$1.$2')
.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')
},
telefone(value) {
return value
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '($1) $2')
.replace(/(\d{4})(\d)/, '$1-$2')
.replace(/(\d{4})-(\d)(\d{4})/, '$1$2-$3')
.replace(/(-\d{4,5})\d+?$/, '$1')
},
cep(value) {
return value
.replace(/\D/g, '')
.replace(/(\d{5})(\d)/, '$1-$2')
.replace(/(-\d{3})\d+?$/, '$1')
}
}
// Pega todos os inputs com data-input e chama o objeto
document.querySelectorAll('input').forEach(($input) => {
const field = $input.dataset.input;
$input.addEventListener('input', (e) => {
e.target.value = mask[field](e.target.value);
}, false)
})