I used the hint of this link here >. But I'm trying to implement something more that I need and can not do.
Next: I need to test if cpf is valid and if it is, I release the register button. For this I test the cpf with the button register disable mode and if it is valid change the condition according to the code below that does not work and I do not know why.
<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 it until I got the value of the function and playing in a variable as it is above in the code.
I tested this by converting this variable to string ( tostring(teste)
).
I definitely do not know what's wrong since testing cpf is ok and when it's valid it does not change the input submit button's condition to enable!