Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { action } from "mobx";
- class masks {
- @action cpfCnpj(input) {
- // Função de máscara para CPF e CNPJ
- const text = input.replace(/[^\d]/g, '')
- let formatedCpfCnpj
- if (text.length <= 3) {
- // Começa a mascára do CPF
- formatedCpfCnpj = text
- } else if (text.length <= 7) {
- formatedCpfCnpj = `${text.slice(0, 3)}.${text.slice(3)}`
- } else if (text.length <= 9) {
- formatedCpfCnpj = `${text.slice(0, 3)}.${text.slice(3, 6)}.${text.slice(6, 9)}`
- } else if (text.length <= 11) {
- // Termina a máscara do CPJ
- formatedCpfCnpj = `${text.slice(0, 3)}.${text.slice(3, 6)}.${text.slice(6, 9)}-${text.slice(9, 11)}`
- } else if (text.length <= 13){
- // Começa a máscara do CNPJ
- formatedCpfCnpj = `${text.slice(0, 2)}.${text.slice(2, 5)}.${text.slice(5, 9)}/${text.slice(9, 13)}`
- } else if (text.length <= 14){
- formatedCpfCnpj = `${text.slice(0, 2)}.${text.slice(2, 5)}.${text.slice(5, 8)}/${text.slice(8, 12)}-${text.slice(12, 14)}`
- }
- return formatedCpfCnpj
- }
- @action telMask(input) {
- // Função de máscara para o Telefone
- const text = input.replace(/[^\d]/g, '')
- if (text.length <= 2) {
- // Começa a mascara do Telefone
- formatedPhone = `(${text.slice(0, 2)})`
- } else if (text.length <= 6) {
- formatedPhone = `(${text.slice(0, 2)}) ${text.slice(2, 6)}`
- } else if (text.length <= 10) {
- // Terminar a mascara do Telefone
- formatedPhone = `(${text.slice(0, 2)}) ${text.slice(2, 6)}-${text.slice(6, 10)}`
- } else if (text.length <= 11) {
- // Começa a mascára do Celular
- formatedPhone = `(${text.slice(0, 2)}) ${text.slice(2, 3)} ${text.slice(3, 7)}-${text.slice(7, 11)}`
- }
- return formatedPhone
- }
- }
- const mask = new masks();
- export default mask;
Advertisement
Add Comment
Please, Sign In to add comment