JQuery Username Validation

拜拜、爱过 提交于 2019-11-29 09:00:28

Set the ajax call option type to 'POST'. Also, AJAX won't block the return isSuccess.

You might look at the remote validation provided by jQuery Validate.

http://docs.jquery.com/Plugins/Validation/Methods/remote#options

Change your rules.

rules: {
        username: {
            required: true,
            minlength: 3,
            remote: 'username_availability.php' // remote check for duplicate username
        }
    },

Just look at the examples.

I'm wondering if this line is the problem:

data: "username=" + username,

Try changing this into a proper array as I don't believe jQuery automatically resolves this sort of format, especially for POST data. Also, set an option of type : 'post' in your $.ajax call since jQuery automatically resolves $.ajax requests to GET and you're using POST in your PHP script.

Try changing the PHP source to return "1" or "0":

include('database_connection.php');                                  
if (isset($_POST['username'])) {                                                               
    $username = mysql_real_escape_string($_POST['username']);                                  
    $check_for_username = mysql_query("SELECT user_id FROM users WHERE username='$username'"); 
    if (mysql_num_rows($check_for_username)) {
        echo "TRUE";                                                                           
    } else {
        echo "FALSE";                                                                           //No Record Found - Username is available
    }
}
?>

and then change your JS to parse that as an integer:

success:
 function(msg) { 
   isSuccess = parseInt(msg);
 }

You could use the following, just change it for your user name (I use the email address as the user name. I use an empty table cell id="emailError" to hold the message.

The jQuery:

    $('#email').keyup(function(){
      $.ajax({
        type: 'POST',
        url: 'ajax.checkEmail.php',
        data: {e:$('#email').val()},
        success: function(data){
          if(data == 'true'){
            $('#emailError').html('This email is already used');
          }else{
            $('#emailError').html('');
          }
        }
      })
    });

The PHP (ajax.checkEmail.php):


  include('inc.php');

  $query  = 'SELECT COUNT(*) FROM tblcustomers WHERE email = "'.$_POST['e'].'"';
  $result = $dbO->getSingleResult($query);

  if($result > 0)
    print 'true';
  else
    print 'false';

This works just check made the same code

      jQuery.validator.addMethod("usernameCheck", function(username) {
      var isSuccess = false;
      $.ajax({ url: "checkusr.php",
      data: "username=" + username,
      async: false,
      success:
      function(msg) { 

      if(msg ==0)
      {
  isSuccess=false;
      }
      else 
      {

       isSuccess=true;
      }
        }


       });

        return isSuccess;
         },"Username not available");

PHP SIDE CODE

           if ($obj->checkUsername($_GET['username'],"artist")> 0 )
       {
       echo 0;
        }
    else 
    {
  echo 1;
 }

  }                                 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!