使用jquery验证插件检查用户名

时间:2021-11-20 16:54:41

I am trying to use the username taken or not using jquery validate plugin. but dosent seems to work. I would like to know where I went wrong, all other validation works fine. except for this.

我正在尝试使用jquery validate plugin获取或不使用的用户名。但dosent似乎很管用。我想知道我哪里出错了,其他的验证都可以。除了这个。

jquery validate plugin page: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

jquery验证插件页面:http://bassistance.de/jqueryplugins/jqueryplugin -validation/

javascript

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

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

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>

thanks in advance.

提前谢谢。

4 个解决方案

#1


0  

To answer the OP, you might want to use $_GET. Also, mysql_num_rows has been deprecated as of PHP 5.5.0, so you'll probably want to update that. You could just check if the query returns false.

要回答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;
?>

#2


2  

I have the same but I use their validation and this to check if username exists. It works great.

我有相同的,但是我使用他们的验证和这个检查用户名是否存在。它的工作原理。

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

#3


1  

First, you have an extra } that is closing your messages option prematurely, leading to the remaining messages being ignored. You should move this } after the confirm_password message block. Second, you should be calling jQuery.validator.format rather than jQuery.format. I believe this is why this particular validation rule doesn't work. Correct these and see if it works for you.

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

#4


0  

Try this......

试试这个……

JS

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

<?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;
  }
?>

May help..

可以帮助. .

#1


0  

To answer the OP, you might want to use $_GET. Also, mysql_num_rows has been deprecated as of PHP 5.5.0, so you'll probably want to update that. You could just check if the query returns false.

要回答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;
?>

#2


2  

I have the same but I use their validation and this to check if username exists. It works great.

我有相同的,但是我使用他们的验证和这个检查用户名是否存在。它的工作原理。

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

#3


1  

First, you have an extra } that is closing your messages option prematurely, leading to the remaining messages being ignored. You should move this } after the confirm_password message block. Second, you should be calling jQuery.validator.format rather than jQuery.format. I believe this is why this particular validation rule doesn't work. Correct these and see if it works for you.

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

#4


0  

Try this......

试试这个……

JS

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

<?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;
  }
?>

May help..

可以帮助. .