php - jQuery.validator.addMethod检查用户名

时间:2015-06-10 13:51:54

标签: php jquery jquery-validate

我是php和jQuery的新手,我用Google搜索了一天但无法解决问题。我想检查是否使用了用户名,但无论我输入什么,我得到的是“用户名已经被使用”。 所以这是我的代码:

$(document).ready(function(){
    $('#registration-form').validate({
    rules: {            
     username: {
            required: true,
            minlength: 3,
            maxlength: 25,
            letters: true,
            checkUsername: true,
        },        
      password: {
            required: true,
            minlength: 6,
            maxlength: 12
        },
    confirmpassword: {
            required: true,
            minlength: 6,
            maxlength: 12,
            equalTo: "#password"
        },        
      email: {
            required: true,
            email: true
        },  

     country:{
            required: true,
     }
            },

        highlight: function(element) {
            $(element).closest('.form-group').addClass('error');
            $(element).removeClass("valid");
        },
        success: function(element) {
            element
            .addClass('valid')
            .closest('.form-group').removeClass('error');
        }
  });

  $(".close, #close").click(function() {
        $("label.error").hide();
        $(".error").removeClass("error");
        $("input.valid").removeClass("valid");
    });

    $.validator.addMethod("letters", function(value, element) {
    return this.optional(element) || value == value.match(/^[a-zA-Z_]+$/);
    },"Letters and underscore only.");

    $.validator.addMethod("checkUsername", function(value, element){
        var response='';
        $.ajax({
            type: "POST",
            url: 'checkname.php',
            data:"username="+value,
            async:false,
            success:function(data){
            response = data;
            }
                });
            if(response == 0)
            {
                return true;
            }
            else
            {
            return false;
            }

    }, "Username is Already Taken");

}); 

这是我的checkname.php

<?php
include 'connect.php';

 if (isSet($_POST['username'])) {                                                               
  $username = mysql_real_escape_string($_POST['username']);                                  
  $check_for_username = mysql_query("SELECT * FROM user WHERE username='$username'"); 
if (mysql_num_rows($check_for_username)>0) {
    echo "false";                                                                           
} else {
    echo "true";                                                                          
}
}


?>

请帮助我,我很沮丧......

1 个答案:

答案 0 :(得分:1)

您应该使用the remote method,它已经过测试并且已经过验证。没有理由重新发明已经完成的事情。

rules: {            
    username: {
        required: true,
        minlength: 3,
        maxlength: 25,
        letters: true,
        remote: {  // value of 'username' field is sent by default
            type: 'POST',
            url: 'checkname.php'
        }
    }
    ....

您的checkname.php

include 'connect.php';

if (isSet($_POST['username']))
{                                                               
    $username = mysql_real_escape_string($_POST['username']);

    $check_for_username = mysql_query("SELECT * FROM user WHERE username='$username'"); 

    if (mysql_num_rows($check_for_username) > 0)
    {
        echo json_encode("Username is Already Taken");   
        // you could even try something like this:
        // echo json_encode($username . " is Already Taken");                                                                        
    } 
    else 
    {
        echo "true";                                                                          
    }
}

注意mysql_已在PHP中弃用,应替换为mysqlipdo_mysql。请参阅:php.net/manual/en/function.mysql-query.php

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