在Jquery中添加JS函数以检查可用或不可用的用户名

时间:2012-09-24 08:01:29

标签: php javascript jquery mysql

我想问一下关于JS函数的问题,现在我希望函数检查用户名 这是我的JS代码:

jQuery.validator.addMethod("noSpace", function(value, element) { 
    return value.indexOf(" ") < 0 && value != ""; 
    });

    $().ready(function()
    {
        $("#signupForm").validate(
        {
            rules:
            {
                full_name: "required",
                username: {
                    required: true,
                    minlength: 2,
                    noSpace: true,
                },
                password: {
                    required: true,
                    minlength: 5
                },
                email: {
                    required: true,
                    email: true
                },
            },
            messages:
            {
                full_name: "Please enter your full name",
                username:
                {
                    required: "Please enter a username",
                    minlength: "Your username must consist of at least 2 characters",
                    noSpace: "No space allowed",
                },

                password:
                {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },

                email: "Please enter a valid email address"
            }
        });
    });

那么如何将该功能添加到检查可用的用户名并在输入文本用户名下面显示(如果可用或不可用),如果可用,用户可以继续提交表单。如果没有,用户无法继续提交表单。

检查我使用PHP语言与Mysql数据库的用户名。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用此示例,请注意如何使用此处检查可用性

$(document).ready(function() {
jQuery.validator.addMethod("checkAvailability",function(value,element){
 var x= $.ajax({
    /*Setting of request*/
 }).responseText;

 if($("message", x).text()=="true") return true;
 else return false;
},"Sorry, this user name is not available");

$("#loginForm").validate({
    rules:  {
        username: {
            required: true,
            minlength: 4,
            checkAvailability: true
        },
    },

    submitHandler: function(form){send()}
});
});