ajax检测用户名是否存在
html代码
javascript 代码
<div class="Mtitle"><span>用户名:</span></div>
<div class="Mright"><input name="username" type="text" id="username" maxlength="20" size="20"
class="colorblue" onfocus="this.className='colorfocus';"
onblur="this.className='colorblue';checkusername(this.value);" />
<span id="checkresult">不超过20个字符</span></div>
<div class="Mright"><input name="username" type="text" id="username" maxlength="20" size="20"
class="colorblue" onfocus="this.className='colorfocus';"
onblur="this.className='colorblue';checkusername(this.value);" />
<span id="checkresult">不超过20个字符</span></div>
javascript 代码
function checkusername(username)
{
var unlen = username.replace(/[^\x00-\xff]/g, "**").length;//若输入的不是ascii值 则算两个字符
if(unlen < 3 || unlen > 20) {
document.getElementById("checkresult").innerHTML = "<font color='#009900'>"
+ (unlen < 3 ? profile_username_tooshort : profile_username_toolong) + "</font>";
return;
}
ajaxRead("tools/ajax.aspx?t=checkusername&username=" + escape(username),
"showcheckresult(obj,'" + username + "');");
}
//escape (charString)是对String对象进行Unicode编码转换,以便其能在所有的计算机上可读。所有空格、标点、重音符号以及其他非 ASCII 字符都用 %xx 编码代替,其中 xx 等于表示该字符的十六进制数。例如,空格返回的是 "%20" 。字符值大于 255 的以 % uxxxx 格式存储。unescape则是逆向操作。
//在ajax.aspx中通过 System.Web.HttpUtility.UrlDecode()方法来获取原始字符
function showcheckresult(obj, username)
{
var res = obj.getElementsByTagName('result');//在xml中查找result标签的节点
var resContainer = document.getElementById("checkresult");
var result = "";
if (res[0] != null && res[0] != undefined)
{
if (res[0].childNodes.length > 1) {
result = res[0].childNodes[1].nodeValue;
} else {
result = res[0].firstChild.nodeValue;
}
}
if (result == "1")
{
resContainer.innerHTML = "<font color='#009900'>对不起,您输入的用户名 \"" + htmlEncode(username, true, 4) + "\" 已经被他人使用或被禁用,请选择其他名字后再试。</font>";
}
else
{
resContainer.innerHTML = profile_username_pass;
}
}
function createXMLHttp() {
if(window.XMLHttpRequest){
return new XMLHttpRequest();
} else if(window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
}
throw new Error("XMLHttp object could be created.");
}
function ajaxRead(file,fun){
var xmlObj = createXMLHttp();
xmlObj.onreadystatechange = function(){
if(xmlObj.readyState == 4){
if (xmlObj.status ==200){
obj = xmlObj.responseXML;//为obj变量赋值 是utf-8编码
eval(fun);//给执行fun字符串中的javascript语句
}
else{
alert("error:[" + xmlObj.status + "]");
}
}
}
xmlObj.open ('GET', file, true);
xmlObj.send (null);
}
{
var unlen = username.replace(/[^\x00-\xff]/g, "**").length;//若输入的不是ascii值 则算两个字符
if(unlen < 3 || unlen > 20) {
document.getElementById("checkresult").innerHTML = "<font color='#009900'>"
+ (unlen < 3 ? profile_username_tooshort : profile_username_toolong) + "</font>";
return;
}
ajaxRead("tools/ajax.aspx?t=checkusername&username=" + escape(username),
"showcheckresult(obj,'" + username + "');");
}
//escape (charString)是对String对象进行Unicode编码转换,以便其能在所有的计算机上可读。所有空格、标点、重音符号以及其他非 ASCII 字符都用 %xx 编码代替,其中 xx 等于表示该字符的十六进制数。例如,空格返回的是 "%20" 。字符值大于 255 的以 % uxxxx 格式存储。unescape则是逆向操作。
//在ajax.aspx中通过 System.Web.HttpUtility.UrlDecode()方法来获取原始字符
function showcheckresult(obj, username)
{
var res = obj.getElementsByTagName('result');//在xml中查找result标签的节点
var resContainer = document.getElementById("checkresult");
var result = "";
if (res[0] != null && res[0] != undefined)
{
if (res[0].childNodes.length > 1) {
result = res[0].childNodes[1].nodeValue;
} else {
result = res[0].firstChild.nodeValue;
}
}
if (result == "1")
{
resContainer.innerHTML = "<font color='#009900'>对不起,您输入的用户名 \"" + htmlEncode(username, true, 4) + "\" 已经被他人使用或被禁用,请选择其他名字后再试。</font>";
}
else
{
resContainer.innerHTML = profile_username_pass;
}
}
function createXMLHttp() {
if(window.XMLHttpRequest){
return new XMLHttpRequest();
} else if(window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
}
throw new Error("XMLHttp object could be created.");
}
function ajaxRead(file,fun){
var xmlObj = createXMLHttp();
xmlObj.onreadystatechange = function(){
if(xmlObj.readyState == 4){
if (xmlObj.status ==200){
obj = xmlObj.responseXML;//为obj变量赋值 是utf-8编码
eval(fun);//给执行fun字符串中的javascript语句
}
else{
alert("error:[" + xmlObj.status + "]");
}
}
}
xmlObj.open ('GET', file, true);
xmlObj.send (null);
}
