Dus*_*usk 1 javascript validation ajax
我想知道如何使用Javascript验证如果用户在创建帐户时输入了任何用户名已存在于数据库中并要求用户键入任何其他用户名?
Cro*_*zin 11
blur事件的监听<input />器.jQuery(我对纯JS太懒了)+ PHP示例代码:
<form ...>
...
<input type="text" name="username" id="input-username" />
<p class="error"></p>
...
$("#input-username").blur(function() {
$.post("/check-username.php", { username: $(this).val() }, function(data) {
if ("0" == data) { /* username in use */
$(this).next("p").text("This username is already in use.</p>");
} else { /* username is fine */
$(this).next("p").empty();
}
});
});
<?php
$username = $_POST['username'];
// check whether given username exists in database
$usernameExists = ...;
echo $usernameExists ? '0' : '1'; // 0 if exists, 1 if not.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1938 次 |
| 最近记录: |