jQuery Validate remote method usage to check if username already exists code example

Example: jQuery Validate remote method usage to check if username already exists

//HTML
$("#signupForm").validate({
  rules: {
    username: {
      required: true,
      minlength: 3,
      remote: "check-username.php"
    }
  },
  messages: {
    username:{
      remote: "This username is already taken! Try another."
    }
  }
});

//PHP code of check-username.php
<?php

header('Content-type: application/json');
$request = $_REQUEST['username'];

$query = mysql_query("SELECT * FROM mmh_user_info WHERE username ='$username'");
$result = mysql_num_rows($query);
if ($result == 0){
$valid = 'true';}
else{
$valid = 'false';
}
echo $valid;
exit;
?>

Tags:

Misc Example