Zaki
Zaki

Reputation: 5600

validation to check database

I am using jquery to do client side validation of a form. This is how I do it:

 $("#registerForm").validate({

            rules: {

                BCNumber : { required: true, minlength: 5, maxlength: 10 },
                Username : { required: true, minlength: 5, maxlength: 10 },
                Password : { required: true, minlength: 5, maxlength: 10 },
                SecurityQuestion : { required: true, minlength: 5, maxlength: 10 },
                SecurityAnswer : { required: true, minlength: 5, maxlength: 10 },

            }

but for one of them I need to check database and if it exist submit the form, I was thinking once the submit button is clicked I can check and pass my true or false value to jquery.

Is this possible to do with jquery, if so can someone please show me?

Upvotes: 0

Views: 751

Answers (3)

Ryley
Ryley

Reputation: 21226

jQuery validate supports a rule type called remote which does what you want. It looks like this:

$("#registerForm").validate({
        rules: {
            Username : { 
              required: true, 
              minlength: 5, 
              maxlength: 10,
              remote: 'check_username.php'
            }
        }
});

Then you create the check_username.php script and have it return a JSON true value if the username is valid, or a false value or error message if it is invalid.

Upvotes: 0

prakashpoudel
prakashpoudel

Reputation: 577

Please try this.

http://jsfiddle.net/Y4fLM/9/

I hope this will help.

Upvotes: 1

StaticVariable
StaticVariable

Reputation: 5283

You can use AJAX

$("#registerForm").submit(function(){
 var  id=//find the value to be checked
   $.get("url?id="+id,function(data){
       if(data=="valid"){
          //submit the form
          }else
             {
            return false;
             }
      })
})

First of all find the value to be checked with database and send it using AJAX xmlhttpRequest.if it is valid than echo valid else return false;

Upvotes: 2

Related Questions