简体   繁体   English

如何使用Javascript验证用户名?

[英]How to validate username using Javascript?

我想知道如何使用Javascript验证用户在创建帐户时输入的任何用户名是否已经存在于数据库中,并要求用户键入其他任何用户名?

  1. Attach listener for blur event for <input /> element. <input />元素的blur事件附加侦听器。
  2. Using AJAX send request to the server (with field value as parameter) 使用AJAX向服务器发送请求(以字段值作为参数)
  3. On the server side check whether given username is already in use or not 在服务器端检查给定的用户名是否已在使用中
  4. Based on server's response display (or not) This username is already in use message 根据服务器的响应显示(或不显示) 此用户名已被使用

jQuery (I'm too lazy for pure JS) + PHP sample code: 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.

The answer is AJAX. 答案是AJAX。 If you must validate against a database, you need to make a call to the server. 如果必须针对数据库进行验证,则需要调用服务器。 The only way to do that ( EDIT : properly) without reloading the page is AJAX. 无需重新加载页面即可执行此操作的唯一方法( 编辑 :正确)是AJAX。 How you implement it will depend upon what javascript libraries you are using, if any, and what your server is like. 如何实现它取决于您使用的是什么JavaScript库(如果有)以及您的服务器是什么样的。 I suggest you do a little searching and reading on it - this is a pretty common use case. 我建议您进行一些搜索和阅读-这是一个非常常见的用例。

Personally, I would use a JQuery validation plugin just to make things simple. 就个人而言,我将使用JQuery验证插件只是为了使事情变得简单。 http://bassistance.de/jquery-plugins/jquery-plugin-validation/ http://bassistance.de/jquery-plugins/jquery-plugin-validation/

But in general it would consist of a small AJAX request to a server (ie. JSON object) with the username and do a 'search' in your database and return either true/false after the user hits enter or tab in the textfield (attach an event listener). 但通常,它会包含一个使用用户名的服务器(即JSON对象)对服务器的小型AJAX请求,并在您的数据库中执行“搜索”,并在用户在文本字段中按Enter或Tab键后返回true / false(附加事件监听器)。 Then within your callback response alter the DOM elements of your choice to indicate to your users whether the account name is already present in the database or not. 然后,在您的回调响应中,更改您选择的DOM元素,以向您的用户指示该帐户名是否已存在于数据库中。

Ajax might not be the only solution, since usernames are generally public. 由于用户名通常是公共的,因此Ajax可能不是唯一的解决方案。 A simple way is to just have an RDF/XML document at some point (which just updates with every new user added) which has a list of all the users on your site that you can easily just traverse with Javascript DOM to see if that user is already in use. 一种简单的方法是仅在某个时候拥有一个RDF / XML文档(该文档会随着添加的每个新用户而更新),其中包含您网站上所有用户的列表,您可以轻松地使用Javascript DOM来遍历该用户已在使用中。 You also make them pay computational power, not you, depending on how nice you are it's an advantage or a dis-advantage. 您还让他们支付计算能力,而不是您自己,这取决于您的好坏,这是优势还是劣势。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM