dave
dave

Reputation: 239

jQuery Validation plugin addMethod

I'm trying to add a new method to jQuery validation plugin with the codes below. My goal is to check whether the email address has already existed in the database(MySQL). If it is, it will inform the user to register for another email address. Somehow, the result that always returns is "Email is already taken".

These are the codes in validate.js:

$(document).ready(function(){
 $.validator.addMethod("uniqueEmail", function(value, element) {
   $.ajax({
      type: "POST",
      url: "availability.php",
      data: value,
      success: function(exist)
      {
       if(exist>0) {
         return true;
       } return false;
      }
  });
 } ,"Email is already taken");
 $('#signup form').validate({
  rules: {
   firstname: {
    required: true,
    minlength:3
   },
   lastname: {
    required: true,
    minlength: 3
   },
   affiliation: {
    required: true,
   },
   occupation: {
    required: true,
   },
   email: {
    required: true,
    email: true,
    uniqueEmail: true
   },
   password: {
    minlength: 6,
    required: true
   },
   repassword: {
    equalTo: "#password"
   }
  },
  messages: {
   firstname: {
    minlength: "Your first name should be more than 3 characters"
   },
   lastname: {
    minlength: "Your last name should be more than 3 characters"
   },
  }, 
  success: function(label) {
   label.text('OK!').addClass('valid');
  }
 });
});

And these are the codes in my php file:

<?php

include_once $_SERVER['DOCUMENT_ROOT'] . '/braddclient/includes/magicquotes.inc.php';
include $_SERVER['DOCUMENT_ROOT'] . '/braddclient/includes/db.inc.php';

$email = strtolower($_POST['email']);
$email = mysqli_real_escape_string($link, $email);
$sql = "SELECT * FROM bradduser WHERE email='$email'";
$result = mysqli_query($link, $sql);
$num = mysqli_num_rows($result);

echo $num;

?>

Upvotes: 1

Views: 1645

Answers (1)

Nick Craver
Nick Craver

Reputation: 630419

You can't quite do this here with a custom method because you need the validation plugin to know this is a remote (or more importantly, an asynchronous) request.

Luckily it has built-in functionality to help. Instead of a custom method you can use remote here, so leave of the custom method altogether and change out this:

uniqueEmail: true

for this:

remote: { url: "availability.php", type: "post" }

Then add the error message to messages like this:

email: { remote: "Email is already taken" }

Also change your PHP side to match, returning true or false, like this:

<?php
  include_once $_SERVER['DOCUMENT_ROOT'] . '/braddclient/includes/magicquotes.inc.php';
  include $_SERVER['DOCUMENT_ROOT'] . '/braddclient/includes/db.inc.php';

  $email = strtolower($_POST['email']);
  $email = mysqli_real_escape_string($link, $email);
  $sql = "SELECT * FROM bradduser WHERE email='$email'";
  $result = mysqli_query($link, $sql);
  if(mysqli_num_rows($result) > 0) {
    echo "false";  //validation fails, email in use
  } else {
    echo "true";   //validation passes
  }
?>

Upvotes: 3

Related Questions