Skip to content

Instantly share code, notes, and snippets.

@AZagatti
Created July 29, 2020 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AZagatti/c853ee3017c95d8d353b79d63d9a4d36 to your computer and use it in GitHub Desktop.
Save AZagatti/c853ee3017c95d8d353b79d63d9a4d36 to your computer and use it in GitHub Desktop.
Validation Functions
/* eslint eqeqeq: [0, "smart"] */
export const getValidTelefone = value => {
const regex = /^[1-9]{2}9?[1-9]\d{7}$/;
const telefone = value.replace(/\D/g, '');
if (regex.test(telefone)) {
return Boolean(telefone);
}
return false;
};
export const getValidEmail = value => {
const regex = /^[\w!#$%&'*+/=?`{|}~^-]+(?:\.[\w!#$%&'*+/=?`{|}~^-]+)*@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$/i;
const email = value;
if (regex.test(email)) {
return Boolean(email);
}
return false;
};
export const getValidCep = value => {
const regex = /[0-9]{5}-[\d]{3}/g;
const cep = value;
if (regex.test(cep)) {
return cep;
}
return null;
};
export const getValidUF = value => {
const regex = /^(?:ac|al|ap|am|ba|ce|df|es|go|ma|mt|ms|mg|pa|pb|pr|pe|pi|rj|rn|rs|ro|rr|sc|sp|se|to)$/i;
const uf = value;
if (regex.test(uf)) {
return uf;
}
return false;
};
export const calculaIdade = dataNasc => {
const dataAtual = new Date();
const anoAtual = dataAtual.getFullYear();
const anoNascParts = dataNasc.split('/');
const diaNasc = anoNascParts[0];
const mesNasc = anoNascParts[1];
const anoNasc = anoNascParts[2];
let idade = anoAtual - anoNasc;
const mesAtual = dataAtual.getMonth() + 1;
// se mês atual for menor que o nascimento, nao fez aniversario ainda; (26/10/2009)
if (mesAtual < mesNasc) {
idade--;
} else {
// se estiver no mes do nasc, verificar o dia
if (mesAtual == mesNasc) {
if (dataAtual.getDate() < diaNasc) {
// se a data atual for menor que o dia de nascimento ele ainda nao fez aniversario
idade--;
}
}
}
return idade;
};
export const getValidCNPJ = cnpj => {
cnpj = cnpj.replace(/[^\d]+/g, '');
if (cnpj == '') return false;
if (cnpj.length != 14) return false;
// Elimina CNPJs invalidos conhecidos
if (
cnpj == '00000000000000' ||
cnpj == '11111111111111' ||
cnpj == '22222222222222' ||
cnpj == '33333333333333' ||
cnpj == '44444444444444' ||
cnpj == '55555555555555' ||
cnpj == '66666666666666' ||
cnpj == '77777777777777' ||
cnpj == '88888888888888' ||
cnpj == '99999999999999'
)
return false;
// Valida DVs
let tamanho = cnpj.length - 2;
let numeros = cnpj.substring(0, tamanho);
const digitos = cnpj.substring(tamanho);
let soma = 0;
let pos = tamanho - 7;
for (let i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
if (pos < 2) pos = 9;
}
let resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11);
if (resultado != digitos.charAt(0)) return false;
tamanho += 1;
numeros = cnpj.substring(0, tamanho);
soma = 0;
pos = tamanho - 7;
for (let i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
if (pos < 2) pos = 9;
}
resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11);
if (resultado != digitos.charAt(1)) return false;
return true;
};
export const getValidCPF = strCPF => {
strCPF = strCPF.replace(/\D/g, '');
let Soma;
let Resto;
Soma = 0;
if (strCPF == '00000000000') return false;
for (let i = 1; i <= 9; i++)
Soma += parseInt(strCPF.substring(i - 1, i)) * (11 - i);
Resto = (Soma * 10) % 11;
if (Resto == 10 || Resto == 11) Resto = 0;
if (Resto != parseInt(strCPF.substring(9, 10))) return false;
Soma = 0;
for (let i = 1; i <= 10; i++)
Soma += parseInt(strCPF.substring(i - 1, i)) * (12 - i);
Resto = (Soma * 10) % 11;
if (Resto == 10 || Resto == 11) Resto = 0;
if (Resto != parseInt(strCPF.substring(10, 11))) return false;
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment