dobrado

Untitled

Feb 18th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import { action } from "mobx";
  2.  
  3. class masks {
  4. @action cpfCnpj(input) {
  5. // Função de máscara para CPF e CNPJ
  6. const text = input.replace(/[^\d]/g, '')
  7. let formatedCpfCnpj
  8.  
  9. if (text.length <= 3) {
  10. // Começa a mascára do CPF
  11. formatedCpfCnpj = text
  12.  
  13. } else if (text.length <= 7) {
  14. formatedCpfCnpj = `${text.slice(0, 3)}.${text.slice(3)}`
  15. } else if (text.length <= 9) {
  16. formatedCpfCnpj = `${text.slice(0, 3)}.${text.slice(3, 6)}.${text.slice(6, 9)}`
  17. } else if (text.length <= 11) {
  18. // Termina a máscara do CPJ
  19. formatedCpfCnpj = `${text.slice(0, 3)}.${text.slice(3, 6)}.${text.slice(6, 9)}-${text.slice(9, 11)}`
  20. } else if (text.length <= 13){
  21. // Começa a máscara do CNPJ
  22. formatedCpfCnpj = `${text.slice(0, 2)}.${text.slice(2, 5)}.${text.slice(5, 9)}/${text.slice(9, 13)}`
  23. } else if (text.length <= 14){
  24. formatedCpfCnpj = `${text.slice(0, 2)}.${text.slice(2, 5)}.${text.slice(5, 8)}/${text.slice(8, 12)}-${text.slice(12, 14)}`
  25. }
  26.  
  27. return formatedCpfCnpj
  28. }
  29.  
  30. @action telMask(input) {
  31. // Função de máscara para o Telefone
  32. const text = input.replace(/[^\d]/g, '')
  33.  
  34. if (text.length <= 2) {
  35. // Começa a mascara do Telefone
  36. formatedPhone = `(${text.slice(0, 2)})`
  37. } else if (text.length <= 6) {
  38. formatedPhone = `(${text.slice(0, 2)}) ${text.slice(2, 6)}`
  39. } else if (text.length <= 10) {
  40. // Terminar a mascara do Telefone
  41. formatedPhone = `(${text.slice(0, 2)}) ${text.slice(2, 6)}-${text.slice(6, 10)}`
  42. } else if (text.length <= 11) {
  43. // Começa a mascára do Celular
  44. formatedPhone = `(${text.slice(0, 2)}) ${text.slice(2, 3)} ${text.slice(3, 7)}-${text.slice(7, 11)}`
  45. }
  46. return formatedPhone
  47. }
  48. }
  49.  
  50. const mask = new masks();
  51.  
  52. export default mask;
Advertisement
Add Comment
Please, Sign In to add comment