Validação de CPF em javascript
Segue abaixo o código para
validação de CPF, em javascript↳JavaScript116 conteúdosJavaScript em 2020: O que esperarDev (Back & Front) · jan 2020Campos públicos e privados em classes JavaScript – O que vem por aí no ESNextDev (Back & Front) · abr 201929 anos de JavaScript!Dev (Back & Front) · jan 2025Ver tudo em Dev (Back & Front) →, todo
comentado. Aproveite!
//Verifica se
string é numérica
function isNum(str)
{
var VBlnIsNum;
VIntTam = str.length;
VBlnIsNum = true;
if (VIntTam == 0)
{
return false;
}
else
{
for (i=0; i < VIntTam; i++)
{
if (str.substring(i,i+1)
< ‘0’ || str.substring(i,i+1) >
‘9’)
{
VBlnIsNum
= false;
}
}
return VBlnIsNum;
}
}
//Função de
validação de CPF
function isCPF(st) {
if (st == "")
return (false);
l = st.length;
//aleterado
para se usuário não digitar os zeros na frente do CPF,
completar sozinho
if ((l == 9) || (l == 8))
{
for (i = l ; i < 10; i++)
{
st = ‘0’ + st
}
}
l = st.length;
st2 = "";
for (i = 0; i < l; i++) {
caracter = st.substring(i,i+1);
if ((caracter >= ‘0’) && (caracter <=
‘9’));
st2 = st2 + caracter;
}
if ((st2.length > 11) || (st2.length < 10))
return (false);
if (st2.length==10)
st2 = ‘0’ + st2;
digito1 = st2.substring(9,10);
digito2 = st2.substring(10,11);
digito1 = parseInt(digito1,10);
digito2 = parseInt(digito2,10);
sum = 0; mul = 10;
for (i = 0; i < 9 ; i++) {
digit = st2.substring(i,i+1);
tproduct = parseInt(digit ,10) * mul;
sum += tproduct;
mul–;
}
dig1 = ( sum % 11 );
if ( dig1==0 || dig1==1 )
dig1=0;
else
dig1 = 11 – dig1;
if (dig1!=digito1)
return (false);
sum = 0;
mul = 11;
for (i = 0; i < 10 ; i++) {
digit = st2.substring(i,i+1);
tproduct = parseInt(digit ,10)*mul;
sum += tproduct;
mul–;
}
dig2 = (sum % 11);
if ( dig2==0 || dig2==1 )
dig2=0;
else
dig2 = 11 – dig2;
if (dig2 != digito2)
return (false);
return (true);
}
Logitech traz o G305 X Superlight ao Brasil com 130 horas de bateria
Logitech começou a vender o G305 X Superlight no mercado brasileiro em 16 de setembro. O preço sugerido ficou em R$ 449,90.








Redação iMasters





