jquery validation plugin and check unique field?

12,719

Solution 1

I reckon you are refering to using an AJAX call (via the plugin) to check for unique email with the server, yea?

I would suggest using the addMethod of the validation plugin to create a custom validation in which you can make an AJAX call (which will be part of the jQuery core).

There's an SO post on this topic which you can explore:
JQuery Validate Plugin - How to create a simple, custom rule?

Do note that you will have to implement the server-side script yourself.

Here's another article which should be useful (using jQuery and PHP):
Check email already exist – Ajax – Jquery

Solution 2

The syntax is simple

$.validator.addMethod("eventName", 
    function(value, element) {
            // condition returns true or false 
    }, "Error message to display");
});

Event name is called in the form

<input type="text" name="name" class="eventName" /> 

Refer this link if any more doubts

jQuery Validate Plugin - How to create a simple custom rule?

Solution 3

If you want to check if the email is unique you can use remote rule. It is from: http://jqueryvalidation.org/remote-method

Example: Makes the email field required, an email and does a remote request to check if the given address is already taken.

$( "#myform" ).validate({
  rules: {
    email: {
    required: true,
    email: true,
    remote: "check-email.php"
   }
  }
});

Example: Makes the email field required, an email and does a remote request to check if the given address is already taken. In addition, the http method is set to “post” and the username is sent alongside the email address.

$( "#myform" ).validate({
  rules: {
   email: {
     required: true,
     email: true,
     remote: {
          url: "check-email.php",
          type: "post",
          data: {
             username: function() {
              return $( "#username" ).val();
              }
          }
     }
  }
 }
});
Share:
12,719
assaqqaf
Author by

assaqqaf

i'm PHP lover... that's all

Updated on June 04, 2022

Comments

  • assaqqaf
    assaqqaf about 2 years

    I am currently using jQuery validation plugin with cakephp in my new project.

    It's working perfectly untill I need to make unique check to email field through ajax to check with the database..

    I didn't know how to make the plugin make a unique validation to email from db.

    thanx