Checking that the CPF is unique with Jquery validate remote

Asked

Viewed 447 times

0

I’m making a form that checks if the number already exists in a database. In my JS I use the Jquery Validate library

$('#form').validate({
        rules:{          
            cpf: {
                required: true,
                cpfBR: true,
                remote: "cpfunico.php"
            },
       },
        messages: { 
            cpf: { 
                remote: "CPF já cadastrado." 
           }
       },     
});

In the Serverside file I have the following request:

 if(procuraCpf($_GET['cpf'])){
       echo true;
       // ja tentei também com echo (json_encode(true));
   } else {
        echo false;
    } 

The function procuraCpf() returns whether it found Cpf in the database and returns to Jquery via a true or false message.

The problem is when the server returns an already registered Cpf, and gives the message via Validate. After that all the Cpf s will give the message "CPF already registered". I inspected the code and identified that Jquery Validate sends the message even before making the request in the database to see if Cpf exists. I don’t understand why this happens.

  • Have you tried in this way?

  • Yes, but the problem remains.

  • Substitute echo true; for echo "true";. The same with false

  • Have you solved your problem? I’m facing the same problem

  • I couldn’t reach friend. I’m validating Cpf when the user saves the form. When I can do this, I will apply in the next version of the system.

No answers

Browser other questions tagged

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