php - 使用 jquery validate 插件检查用户名

标签 php jquery mysql

我正在尝试使用 jquery 验证插件使用或不使用的用户名。但剂量似乎有效。我想知道我哪里出错了,所有其他验证工作正常。除了这个。

jquery 验证插件页面:http://bassistance.de/jquery-plugins/jquery-plugin-validation/

JavaScript

<script type="text/javascript">
        $(document).ready(function(){
        $("#register").validate({
            debug: false,
            rules: {
                username: {
                required: true,
                minlength: 2,
                remote: "users.php"
            },
                email: {
                    required: true,
                    email: true
                },
            password: {
                required: true,
                minlength: 5
            },
            confirm_password: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            }   
            },
            messages: {
                username: {
                required: "Please enter a username",
                minlength: "Your username must consist of at least 2 characters",
                remote: jQuery.format("{0} is already in use")
            },
                email: "A valid email will help us get in touch with you.",
            },
            password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            confirm_password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long",
                equalTo: "Please enter the same password as above"
            },
            submitHandler: function(form) {
                // do other stuff for a valid form
                $.post('adduser.php', $("#register").serialize(), function(data) {
                $("#register").fadeOut('fast', function(){
                    $('#results').html(data);
                });
                });
            }
        });
    });
</script>

用户.php

<?php
$request = $_REQUEST['username'];

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

注册.php

<form name="register" id="register" method="post" action="">
<section>
<label for="username">Username:</label>
<div>
<input type="text" tabindex="1" class="input" id="username" name="username" value=""/>
</div>
</section>
<!--#-->
<section>
<label for="email">email</label>
<div>
<input type="text" tabindex="2" class="input" id="email" name="email" value=""/>
</div>
</section>
<!--#-->
<section>
<label for="password">Password</label>
<div>
<input type="password" tabindex="3" class="input" id="password" name="password" value=""/>
</div>
</section>
<!--#-->
<section>
<label for="confirm_password">Confirm Password</label>
<div>
<input type="password" tabindex="4" class="input" id="confirm_password" name="confirm_password" value=""/>
</div>
</section>
<!--#-->
<br/>
<input type="submit" tabindex="5" id="submit" value="REGISTER" />
</form>
<div id="results"> </div>

提前致谢。

最佳答案

我有相同的但我使用他们的验证和 this检查用户名是否存在。效果很好。

<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
        //the min chars for username
        var min_chars = 3;

        //result texts
        var characters_error = 'username must have atleast 3 characters';
        var checking_html = 'searching...';

        //when button is clicked
        $('#check_username_availability').click(function(){
            //run the character number check
            if($('#username').val().length < min_chars){
                //if it's bellow the minimum show characters_error text
                $('#username_availability_result').html(characters_error);
            }else{          
                //else show the cheking_text and run the function to check
                $('#username_availability_result').html(checking_html);
                check_availability();
            }
        });


  });

//function to check username availability   
function check_availability(){

        //get the username
        var username = $('#username').val();

        //use ajax to run the check
        $.post("checkuser.php", { username: username },
            function(result){
                //if the result is 1
                if(result == 1){
                    //show that the username is available
                    $('#username_availability_result').html('<span class="is_available"><b>' +username + '</b> is available</span>');
                    //this is the id of my save button it'll display if available
                    $('#newacct').css("display", "inline");
                }else{
                    //show that the username is NOT available
                    $('#username_availability_result').html('<span class="is_not_available"><b>' +username + '</b> is not available</span>');
                }
        });

}  
</script>

关于php - 使用 jquery validate 插件检查用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8457740/

相关文章:

mysql - Laravel 查询生成器加入

php if语句(如何查看用户输入重复记录)

javascript - 来自 php 的 JSON 解码信息

javascript - 如何删除子元素

javascript - WordPress javascript 加载顺序

MySQL - 如何执行一次子查询来计算许多其他字段?

php - 验证 iOS 发送的 PHP 中的自定义 HTTP header

php - 使用 php 从 mySQL 数据创建实例

php - 使用 MySQL 数据重新填充/编辑 HTML 表单输入

mysql - 如何丢弃内连接表中的值?