2

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

o.k.w
  • 25,490
  • 6
  • 66
  • 63
assaqqaf
  • 1,575
  • 4
  • 21
  • 38

4 Answers4

3

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

Community
  • 1
  • 1
o.k.w
  • 25,490
  • 6
  • 66
  • 63
1

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?

Community
  • 1
  • 1
Eugine Joseph
  • 1,552
  • 3
  • 18
  • 40
1

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();
              }
          }
     }
  }
 }
});
lovelyramos
  • 9,379
  • 1
  • 17
  • 18
0

Try something like

  $.validator.addMethod("unique",
            function(value, element, params) {
                var isUnique = false;
                if(value == '')
                        return isUnique;

                id_send= '';
                if(params[1] !='')
                    id_send ='id='+params[1]+'&';

                $.ajax({
                    url: "path"+params[2],
                    type : 'GET',
                    async: false,
                    data: id_send+'field=' + params[0] + "&value=" + value,
                    dataType: 'json',
                    cache: true,
                    success: function(data){
                        isUnique = data;
                    }
                });

                return isUnique;

            },
            jQuery.validator.format("Value already in use")
    );

In the above code:

  • path is the root path of your application;
  • params[0] is the name of attribute to check unique;
  • params[1] is the id of the object, if you want to check in edit too, so exclude himself;
  • params[2] is the path to the php file that gonna check.

Resulting in something like:

 rules:
        {
            email: {
                required: true,
                unique:['email', $('#user_id').val(),'uniqueemail.php'],
                email: true
            },

The PHP uniqueemail.php, search for the value in field email, if return empty or the user with id equals $('#user_id').val() so return true, else return false.

Note that the async attribute is set false, this is a set back but is gonna do the job.