Validate more than one input CPF field

Asked

Viewed 400 times

0

I have this script for CPF validation. But in my form, there is more than one field that requires CPF validation, within a table that adds as you press the add button. How should I make the script validate these other two fields ?

Script Validation CPF

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();                        
                    for(var i =0;i<40;i++) {
                        var temp_cpf = CPF.gera();                          
                            //document.write(temp_cpf+" = "+CPF.valida(temp_cpf)+"<br>");
                    }

                    $("#input").keypress(function(){
                        $("#resposta").html(CPF.valida($(this).val()));
                    });

                    $("#input").blur(function(){
                        $("#resposta").html(CPF.valida($(this).val()));
                    }); 

Field within the Table

<td><input class="form-control" style="width: 180px" type="text" id="cpfFilial" name="cpfFilial" mask="000.000.000-00" >

And

<td><input class="form-control" type="text" style="width: 100%;" id="cpfContCliente" name="cpfContCliente" mask="000.000.000-00"></td>

The first CPF field that is outside tables, is just a normal input, this validating correctly.

The fields that also need validation are: cpfFilial and cpfContCliente

When you press the "new" button on the table, they look like this :

cpfFilial___1 / cpfFilial___2 / cpfContCliente___1 / cpfContCliente___2

  • It seems to me that the code you posted is incomplete. That’s the complete code?

  • The Script? is complete yes !

1 answer

0


I noticed you’re using jQuery, I believe you can do it that way:

$(document).on('blur keyup', "input[name^='cpf']",function() {
        $("#resposta").html(CPF.valida($(this).val()));
});

^ is a criterion selector Starts with.

  • Lucas, it didn’t work!

  • Any error in console?

  • No. console in braco. and the field that was working properly, stopped working.

  • It is possible to make a Fiddle?

  • I simplified the HTML and left the last two fields inside the tables of which the last two inputs are inside. https://jsfiddle.net/kapinheiro/nsrsc3xe/

  • Remembering that the first input works correctly even though it does not appear in jsfiddle

  • Sorry, I failed to use aspas. Check now: https://jsfiddle.net/lbclucascosta/nsrsc3xe/1/

  • Lucas, it only worked the first input.

  • you commented that you were wrong with the quotation marks, but the fiddle you sent, is the same as the first.

  • Not @Karinapinheiro, please note that you are "input[name^='cpf']" and not 'input[name^='cpf']' as it was before. I put a valid CPF in the third input it validated, if I withdraw any number it exchanges the message to invalid CPF. What is not working? How does the message appear? http://imgur.com/a/K8lAl

  • Lucas, it’s working, yes. What I hadn’t noticed was that the answer appears there in the first input. I thought it appeared in front of the input that was typed.

  • And I noticed something else, the ideal is to use keyup and not keypress, because keypress gets up to the penultimate typed value and not the current one, IE, it will always consider the value less what was last typed. Check now: https://jsfiddle.net/nsrsc3xe/2/

  • 1

    @Karinapinheiro in jQuery It is not necessary to create a on for each event, simply group as follows: https://gist.github.com/brcontainer/11d3d47d9ba7ce62b2566ddbd0493f96

  • Boa @Guilhermenascimento, edited with your suggestion

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.