-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmascaras.js
More file actions
146 lines (95 loc) · 3.5 KB
/
Copy pathmascaras.js
File metadata and controls
146 lines (95 loc) · 3.5 KB
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
// ------------------- DATA -------------------
var campoData = document.getElementById('id_data');
campoData.addEventListener('input', function () {
this.value = mascaraData(this.value);
});
function mascaraData(data) {
data = data.replace(/\D/g, '');
if (data.length > 2) {
data = data.replace(/^(\d{2})(\d)/g, '$1/$2');
if (data.length > 5) {
data = data.replace(/^(\d{2})\/(\d{2})(\d{1,4})$/, '$1/$2/$3');
}
}
return data;
}
// -------------------- CEP ---------------------
var inputCEP = document.getElementById('id_cep');
inputCEP.addEventListener('input', function () {
var cep = this.value.replace(/\D/g, '');
if (cep.length === 8) {
cep = cep.substring(0, 5) + '-' + cep.substring(5);
}
this.value = cep;
});
// ----------------------- CPF/CNPJ -----------------------
document.getElementById('id_cpfcnpj').addEventListener('input', function (event) {
let value = event.target.value.replace(/\D/g, '');
if (value.length === 11) {
event.target.value = value.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, '$1.$2.$3-$4');
} else if (value.length === 14) {
event.target.value = value.replace(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/, '$1.$2.$3/$4-$5');
}
});
// ------------------------ TELEFONE FIXO / CELULAR ---------------------
document.getElementById('id_telefone').addEventListener('input', function (event) {
let inputValue = event.target.value;
inputValue = inputValue.replace(/\D/g, '');
if (inputValue.length === 11) {
inputValue = inputValue.replace(/^(\d{2})(\d{5})(\d{4})$/, '($1) $2-$3');
} else {
inputValue = inputValue.replace(/^(\d{2})(\d{4})(\d{4})$/, '($1) $2-$3');
}
event.target.value = inputValue;
});
// ------------------------ PORCENTAGEM ---------------------
function mascaraPorcentagem(valor) {
valor = valor.replace(/\D/g, '');
valor = valor + '%';
return valor;
}
var campoPorcentagem = document.getElementById('porcentagem');
campoPorcentagem.addEventListener('input', function () {
this.value = mascaraPorcentagem(this.value);
});
// ------------------------ MÊS E ANO ---------------------
function mascaraMesAno(valor) {
valor = valor.replace(/\D/g, '');
valor = valor.slice(0, 6);
if (valor.length > 2) {
valor = valor.slice(0, 2) + '/' + valor.slice(2);
}
return valor;
}
var campoMesAno = document.getElementById('mesAno');
campoMesAno.addEventListener('input', function () {
this.value = mascaraMesAno(this.value);
});
// ------------------------ HORAS E MINUTOS ---------------------
function mascaraHorasMinutos(valor) {
valor = valor.replace(/\D/g, '');
valor = valor.slice(0, 4);
if (valor.length > 2) {
valor = valor.slice(0, 2) + 'h' + valor.slice(2);
}
if (valor.length > 4) {
valor = valor.slice(0, 5) + 'min';
}
return valor;
}
var campoHorasMinutos = document.getElementById('horasMinutos');
campoHorasMinutos.addEventListener('input', function () {
this.value = mascaraHorasMinutos(this.value);
});
// ------------------------ MOEDA (R$) ---------------------
function mascaraMoeda(valor) {
valor = valor.replace(/\D/g, '');
valor = valor.replace(/(\d)(\d{2})$/, '$1,$2');
valor = valor.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.');
valor = 'R$ ' + valor;
return valor;
}
var campoValor = document.getElementById('valor');
campoValor.addEventListener('input', function () {
this.value = mascaraMoeda(this.value);
});