2
I used that tip link here. But I’m trying to implement something else I need and I can’t.
Next: I need to test if Cpf is valid and if it is, release the sign-up button. For this I test Cpf with the sign up mode disable button and if it is valid change the condition according to the code below that does not work and do not know pq.
<form>
<p><input type="text" id="cpf" name="cpf"/><span id="resposta"></span></p>
<p><input id="cadastrar" name="cadastrar" type="submit" value="Cadastrar" disabled /></p>
</form>
<script>
function CPF(){"user_strict";function r(r){for(var t=null,n=0;9>n;++n)t+=r.toString().charAt(n)*(10-n);var i=t%11;return i=2>i?0:11-i}function t(r){for(var t=null,n=0;10>n;++n)t+=r.toString().charAt(n)*(11-n);var i=t%11;return i=2>i?0:11-i}var n="CPF Inválido",i="CPF Válido";this.gera=function(){for(var n="",i=0;9>i;++i)n+=Math.floor(9*Math.random())+"";var o=r(n),a=n+"-"+o+t(n+""+o);return a},this.valida=function(o){for(var a=o.replace(/\D/g,""),u=a.substring(0,9),f=a.substring(9,11),v=0;10>v;v++)if(""+u+f==""+v+v+v+v+v+v+v+v+v+v+v)return n;var c=r(u),e=t(u+""+c);return f.toString()===c.toString()+e.toString()?i:n}}
var CPF = new CPF();
$(document).ready(function(){
$("#cpf").keypress(function(){
var teste= CPF.valida($(this).val());
$("#resposta").html(teste);
if(teste == "CPF Válido"){
$("#submit").removeAttr("disabled");
}else {
alert("O campo cpf é inválido! Preencha com um CPF válido por favor.");
return false;
}
});
$("#cpf").blur(function(){
var teste= CPF.valida($(this).val());
$("#resposta").html(teste);
if(teste == "CPF Válido"){
$("#submit").removeAttr("disabled");
} else {
alert("O campo cpf é inválido! Preencha com um CPF válido por favor.");
return false;
}
});
});
</script>
What returns from the function is a correct string? Valid CPF or Invalid CPF. However, it does not enable the button at all. I tested until receiving the value of the function and playing in a variable as it is above in the code.
I tested converting this variable to string ( tostring(teste)
).
I definitely do not know what is wrong once testing Cpf is ok and when it is valid do not change the condition of the input button Submit to enable!
You can replace these conditional blocks with
$("#cadastrar").attr("disabled", teste == 'CPF Inválido');
:).– Renan Gomes