jquery 实时检测帐号是否被注册
这个应该是一个非常实用的代码,当然网上也很多,本例可以作为参考
数据库也是相当的简单就2个字段
CREATE TABLE `username_list` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(60) character set latin1 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
js代码
JavaScript Code复制内容到剪贴板
- <script type="text/javascript">
- $(document).ready(function() {
- $("#username").keyup(function (e) {
- //removes spaces from username
- $(this).val($(this).val().replace(/s/g, ''));
- var username = $(this).val();
- if(username.length < 4){$("#user-result").html('');return;}
- if(username.length >= 4){
- $("#user-result").html('<img src="imgs/ajax-loader.gif" />');
- $.post('check_username.php', {'username':username}, function(data) {
- $("#user-result").html(data);
- });
- }
- });
- });
- </script>
主要代码
XML/HTML Code复制内容到剪贴板
- <div id="registration-form">
- <label for="username">输入帐号:
- <input name="username" type="text" id="username" maxlength="15">
- <span id="user-result"></span>
- </label>
- <br>输入的字符大于等于4才会检测
- </div>
check_username.php
PHP Code复制内容到剪贴板
- <?php
- include_once("conn.php");
- //check we have username post var
- if(isset($_POST["username"]))
- {
- //check if its ajax request, exit script if its not
- if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
- die();
- }
- //trim and lowercase username
- $username = strtolower(trim($_POST["username"]));
- //sanitize username
- $username = filter_var($username, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
- //check username in db
- $results = mysql_query("SELECT id FROM username_list WHERE username='$username'");
- //return total count
- $username_exist = mysql_num_rows($results); //total records
- //if value is more than 0, username is not available
- if($username_exist) {
- die('<img src="imgs/not-available.png" />');
- }else{
- die('<img src="imgs/available.png" />');
- }
- //close db connection
- }
- ?>
原文地址:http://www.freejs.net/article_biaodan_15.html