简体   繁体   English

在注册表单中检查用户名

[英]Check Username in Registration form

In the following code, I want the username to be checked if it's available or not before submiting the form. 在下面的代码中,我希望在提交表单之前检查用户名是否可用。 I used onchange() event with the username field. 我在用户onchange()使用了onchange()事件。

HTML Code: HTML代码:

<form method="post" action="RegConf.php">
    <p>
        <input type="text" id="username" name="username" value="" placeholder="Username" onchange="check_ava()"> 
        <span id='ava_result'>
    </p>
    <p>
        <input type="password" id="password" name="password" value="" placeholder="Password">
    </p>
    <p>
        <input type="password" id="confPassword" name="confPassword" value="" placeholder="Confirm Password" onchange="check()"> 
        <span id='message'>
    </p>
    <p>
        <input type="text" name="email" value="" placeholder="Email">
    </p>
    <p>
        <input type="text" name="phone" value="" placeholder="Phone">
    </p>
    <p>
        <input type="text" name="address" value="" placeholder="Address">
    </p>
    <p class="submit">
        <input type="submit" id="submit" name="commit" value="Submit">
        <button type="reset" value="Reset">Reset</button>
    </p>
</form>
function check_ava() {  
    var username = document.getElementById('username').value;  
    $.post("check_username.php", { username: username }, function(result){   
        if (result == 1) {  
            document.getElementById('ava_result').innerHTML = "Username is available";
            document.getElementById('submit').disabled = false;
        } else {  
            document.getElementById('ava_result').innerHTML = "Username is not available";
            document.getElementById('submit').disabled = true;
        }  
    });  
} 
<?php
    include 'DBConnection.php';

    $username = mysql_real_escape_string($_POST['username']);  
    $result = mysql_query('select Name from User where Name = "'. $username .'"');  
    if (mysql_num_rows($result) > 0) {  
        echo 0;  
    } else {  
        echo 1;  
    }
?>

The problem is that it is not working and not checking the username. 问题是它不起作用并且没有检查用户名。

You Have to use remote method: 您必须使用远程方法:

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>  
<script> 
    $(function() {
                $('#form-validation').validate({

                    rules: {

                        username: {
                            required: true,
                            remote: {
                            url: "check_username.php",
                            type: "post"
                     }
                        },

                    },
                    messages: {
                      val_number: 'Please enter a Value!',

                    }
                });  
            });
</script>

and your check_username.php is, 而您的check_username.php是,

$registeredName[] = //select all names in your databse using select and store here as array, 

$requestedName  = $_POST['username'];
if(in_array($requestedName,$registeredName))
{
    echo 'false';
}
else
{
    echo 'true';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查表单提交中是否存在用户名 - check if username exists on form submit 反应式表单,检查用户名是否存在 - Reactive Form , check if username exists 检查 MongoDB 中是否已存在用于注册的用户名/电子邮件的有效方法 - Efficient way to check if username/email already exist in MongoDB for registration 在注册表单中使用同步JAX来检查用户名的可用性 - Using synchronous JAX in registration form for checking username availability 如何检查用户名是否以表格形式即时存在? - How to check if username exists on the fly in a form? 提交表单前如何检查唯一的用户名 - How to check unique username before form submission 在登录表单(jQuery)中检查用户名和密码 - Check username and password in Login Form (jQuery) Onblur或onchange检查,在注册表单中使用php函数 - Onblur or onchange check, using a php function in a registration form JavaScript正则表达式检查Facebook注册表格等电子邮件格式 - JavaScript Regex to check email format like Facebook registration form 如何确保每个用户的注册表格都是唯一的,而不是相同的用户名,电子邮件或地址等? - How to ensure registration form is unique for each user rather than the same username or email or address etc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM