Implementando outros formatos
This commit is contained in:
parent
2bb02c49fb
commit
4977ef65d4
2 changed files with 51 additions and 2 deletions
|
@ -2,6 +2,14 @@
|
|||
|
||||
Biblioteca com funções para formatação de dados.
|
||||
|
||||
### Formatos Suportados:
|
||||
|
||||
CEP: 12.123-000
|
||||
CNPJ: 12.123.456/0001-00
|
||||
CPF: 123.456.789-00
|
||||
CPF/CNPJ: Retorna no CPF ou CNPJ de Acordo com o Tamanho da Entrada.
|
||||
Conta Bancária: 1234-5
|
||||
|
||||
### Exemplos
|
||||
|
||||
```js
|
||||
|
|
|
@ -2,7 +2,7 @@ module.exports = {
|
|||
// Entrada: 12345000 Saída: 12.345-000
|
||||
formatarCep(txt) {
|
||||
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
|
||||
if (!soNumeros) onChange(soNumeros);
|
||||
if (!soNumeros) return soNumeros;
|
||||
|
||||
let cep = '';
|
||||
cep += soNumeros.slice(0, 2);
|
||||
|
@ -17,7 +17,7 @@ module.exports = {
|
|||
// Entrada: 12345678000100 Saida: 12.345.678/0001-00
|
||||
formatarCnpj(txt) {
|
||||
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
|
||||
if (!soNumeros) onChange(soNumeros);
|
||||
if (!soNumeros) return soNumeros;
|
||||
|
||||
let cnpj = '';
|
||||
cnpj += soNumeros.slice(0, 2)
|
||||
|
@ -35,5 +35,46 @@ module.exports = {
|
|||
cnpj += '-' + soNumeros.slice(12, 14)
|
||||
|
||||
return cnpj
|
||||
},
|
||||
// Entrada: 12345678900 Saída: 123.456.789-00
|
||||
formatarCpf(txt) {
|
||||
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
|
||||
if (!soNumeros) return soNumeros;
|
||||
|
||||
let cpf = '';
|
||||
cpf += soNumeros.slice(0, 3)
|
||||
|
||||
if (soNumeros.length > 3)
|
||||
cpf += '.' + soNumeros.slice(3, 6)
|
||||
|
||||
if (soNumeros.length > 6)
|
||||
cpf += '.' + soNumeros.slice(6, 9)
|
||||
|
||||
if (soNumeros.length > 9)
|
||||
cpf += '-' + soNumeros.slice(9, 11)
|
||||
|
||||
return cpf;
|
||||
},
|
||||
formatarCpfCnpj(txt) {
|
||||
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
|
||||
if (soNumeros.length <= 11) {
|
||||
return this.formatarCpf(txt);
|
||||
} else {
|
||||
return this.formatarCnpj(txt);
|
||||
}
|
||||
},
|
||||
// Entrada: 12345 Saída: 1234-5
|
||||
formatarContaBancaria(txt) {
|
||||
const soNumeros = e.target.value.replace(/[^\d]/g, '')
|
||||
if (!soNumeros) return soNumeros;
|
||||
|
||||
let conta = '';
|
||||
if (soNumeros.length > 1) {
|
||||
conta = soNumeros.slice(0, soNumeros.length - 1) + '-' + soNumeros[soNumeros.length - 1];
|
||||
} else {
|
||||
conta = soNumeros;
|
||||
}
|
||||
|
||||
return conta;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue