Skip to content

Instantly share code, notes, and snippets.

@jacksonfdam
Created June 1, 2012 16:31
Show Gist options
  • Save jacksonfdam/2853381 to your computer and use it in GitHub Desktop.
Save jacksonfdam/2853381 to your computer and use it in GitHub Desktop.
jQuery Ajax Validation Use the Remote Rule
//VALIDATE USER EMAIL
$(':input[name="uAcc"]').rules("add",
{
  "remote" :
  {
      url: validateEmail.php',
      type: "post",
      data:
      {
          emails: function()
          {
              return $('#register-form :input[name="email"]').val();
          }
      }
  }
});
//The old add custom method way
//VALIDATE USER EMAIL
$.validator.addMethod("validateUserEmail", function(value, element)
{
    var inputElem = $('#register-form :input[name="email"]'),
        data = { "emails" : inputElem.val() },
        eReport = ''; //error report
 
    $.ajax(
    {
        type: "POST",
        url: validateEmail.php,
        dataType: "json",
        data: data,
        success: function(data)
        {
            if (data !== 'true')
            {
              return '<p>This email address is already registered.</p>';
            }
            else
            {
               return true;
            }
        },
        error: function(xhr, textStatus, errorThrown)
        {
            alert('ajax loading error... ... '+url + query);
            return false;
        }
    });
 
}, '');
 
$(':input[name="email"]').rules("add", { "validateUserEmail" : true} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment