useInputRules.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { useI18n } from "vue-i18n";
  2. export const useInputRules = () => {
  3. const { t } = useI18n();
  4. const emailPattern =
  5. /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  6. const passwordPattern = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/;
  7. const cepPattern = /^[0-9]{5}-[0-9]{3}$/;
  8. const inputRules = {
  9. required: (value) => !!value || t("validation.rules.required"),
  10. requiredNumber: (value) => !isNaN(value) || t("validation.rules.required"),
  11. requiredHideMessage: (value) => !!value,
  12. min: (min) => (value) =>
  13. value.length >= min ||
  14. `${t("validation.rules.min")} ${min} ${t("validation.rules.characters")}`,
  15. max: (max) => (value) =>
  16. value.length <= max ||
  17. `${t("validation.rules.max")} ${max} ${t("validation.rules.characters")}`,
  18. minValue: (min) => (value) =>
  19. value >= min || `${t("validation.rules.min")} ${min}`,
  20. maxValue: (max) => (value) =>
  21. value <= max || `${t("validation.rules.max")} ${max}`,
  22. email: (value) =>
  23. !value || emailPattern.test(value) || t("validation.rules.email"),
  24. emails: (value) => {
  25. if (!value) return true;
  26. const emails = value.split(";").map((email) => email.trim());
  27. return (
  28. emails.every((email) => inputRules.email(email) === true) ||
  29. t("validation.rules.email")
  30. );
  31. },
  32. cpf: (value) => !value || isValidCPF(value) || t("validation.rules.cpf"),
  33. cnpj: (value) => !value || isValidCNPJ(value) || t("validation.rules.cnpj"),
  34. samePassword: (otherValue) => (value) =>
  35. value === otherValue || t("validation.rules.same_password"),
  36. password: (value) =>
  37. !value || passwordPattern.test(value) || t("validation.rules.password"),
  38. cep: (value) => {
  39. if (!value) return true;
  40. return cepPattern.test(value) || t("validation.rules.cep");
  41. },
  42. notSameDocument: (allDocuments) => (value) => {
  43. if (!value) return true;
  44. let found = 0;
  45. for (const doc of allDocuments) {
  46. if (doc == value) {
  47. found++;
  48. }
  49. if (found > 1) {
  50. return t("validation.rules.not_same_document");
  51. }
  52. }
  53. return true;
  54. },
  55. };
  56. return {
  57. inputRules,
  58. };
  59. };
  60. function isValidCPF(cpf) {
  61. if (!cpf) return false;
  62. cpf = cpf.replace(/[^\d]+/g, "");
  63. if (cpf.length !== 11) return false;
  64. if (/^(\d)\1+$/.test(cpf)) return false;
  65. let sum = 0;
  66. for (let i = 0; i < 9; i++) sum += parseInt(cpf.charAt(i)) * (10 - i);
  67. let rev = 11 - (sum % 11);
  68. if (rev === 10 || rev === 11) rev = 0;
  69. if (rev !== parseInt(cpf.charAt(9))) return false;
  70. sum = 0;
  71. for (let i = 0; i < 10; i++) sum += parseInt(cpf.charAt(i)) * (11 - i);
  72. rev = 11 - (sum % 11);
  73. if (rev === 10 || rev === 11) rev = 0;
  74. if (rev !== parseInt(cpf.charAt(10))) return false;
  75. return true;
  76. }
  77. function isValidCNPJ(cnpj) {
  78. if (!cnpj) return false;
  79. cnpj = cnpj.replace(/[^\d]+/g, "");
  80. if (cnpj.length !== 14) return false;
  81. if (/^(\d)\1+$/.test(cnpj)) return false;
  82. let length = cnpj.length - 2;
  83. let numbers = cnpj.substring(0, length);
  84. let digits = cnpj.substring(length);
  85. let sum = 0;
  86. let pos = length - 7;
  87. for (let i = length; i >= 1; i--) {
  88. sum += parseInt(numbers.charAt(length - i)) * pos--;
  89. if (pos < 2) pos = 9;
  90. }
  91. let result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
  92. if (result !== parseInt(digits.charAt(0))) return false;
  93. length = length + 1;
  94. numbers = cnpj.substring(0, length);
  95. sum = 0;
  96. pos = length - 7;
  97. for (let i = length; i >= 1; i--) {
  98. sum += parseInt(numbers.charAt(length - i)) * pos--;
  99. if (pos < 2) pos = 9;
  100. }
  101. result = sum % 11 < 2 ? 0 : 11 - (sum % 11);
  102. if (result !== parseInt(digits.charAt(1))) return false;
  103. return true;
  104. }