使用jquery validate插件检查用户名

时间:2011-12-10 15:51:42

标签: php jquery mysql

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

jquery validate plugin page: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>

users.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;
?>

register.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>

提前感谢。

4 个答案:

答案 0 :(得分:2)

我有相同但我使用他们的验证和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>

答案 1 :(得分:1)

首先,您有一个额外的},它会过早关闭您的邮件选项,导致其余邮件被忽略。您应该在confirm_password消息块之后移动此}。其次,您应该致电jQuery.validator.format而不是jQuery.format。我相信这就是为什么这个特定的验证规则不起作用的原因。纠正这些并看看它是否适合你。

答案 2 :(得分:0)

要回答OP,您可能想要使用$ _GET。此外,自PHP 5.5.0起,mysql_num_rows已被弃用,因此您可能希望更新它。您可以检查查询是否返回false。

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

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

答案 3 :(得分:0)

试试这个......

  

JS

 $("#register").validate({
        debug: false,
        rules: {
            username: {
            required: true,
            minlength: 6,
            remote: {
              url: "Your PHP",
              type: "post",
              data: {
                funct: 'user_check',
                username: function() {
                  return $( "#username" ).val();
                }
              }
            }
        }, 
        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")
        }  
    });
});
  

PHP

<?php
  function user_check(){
    $username = $_POST['username'];
    $query = mysql_query("SELECT * FROM users WHERE username ='$username'");
    if ($query == false){
      $valid = 'true';} // username available
    else{
      $valid = 'false'; // username unavailable
    }
      echo $valid;
  }
?>

可能有帮助......