Js注册验证代码
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Js验证测试代码</title>
<style> body{ font-size:12px;}</style>
<script language="JavaScript" type="text/JavaScript" >
function focusInFirst()
{
document.forms[0].elements[0].focus();
} /*判断用户输入是否为空*/
function isEmpty(ui)
{
if(ui==null||ui==""){return false};
}
/*取得用户输入的字符串的长度*/
function getLength(ui)
{
var i,sum=0;
for(i=0;i<ui.length;i++)
{
if ((ui.charCodeAt(i)>=0)&&(ui.charCodeAt(i)<=255))
sum++;
else
sum+=2;
}
return sum;
} /*是否为数字、字母或下划线*/
function isNLU(ui)
{
var valid=/^\w+$/;
return(valid.test(ui))
} /*判断是否为身份证号码*/
function isIdno(ui)
{
var valid=/(^\d{16}$)|(^\d{18}$)/;
return (isEmpty(ui)||valid.test(ui));
} /*判断是否为邮政编码*/
function isPostCode(ui)
{
var valid=/^\d{6}$/;
return (isEmpty(ui)||valid.test(ui));
} /*判断是否为固定电话*/
function isChinaTel(ui)
{
var valid=/^0\d{2,3}\-\d{7,8}$/;
return (isEmpty(ui)||valid.test(ui));
} /*判断是否为移动电话*/
function isChinaMob(ui)
{
var valid=/^0?13[0,1,3,5,6,8,9]\d{8}$/;
return (isEmpty(ui)||valid.test(ui));
} /*判断是否为电话,只能为固定电话或移动电话*/
function isTel(ui)
{
var valid=/(^0\d{2,3}\-\d{7,8}$)|(^0?13[0,1,3,5,6,8,9]\d{8}$)/;
return (isEmpty(ui)||valid.test(ui));
} /*判断是否为邮件*/
function isMail(ui)
{
if(isEmpty(ui)){return true;}
var notValid=/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(^\-)|(\-\.)|(\.\-)/;
var valid=/^.+\@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}$/;
return (!notValid.test(ui)&&valid.test(ui));
} /*用户输入字符串长度是否在两值之间*/
function isLenBetween(ui,minl,maxl)
{
return (ui>=minl&&ui<=maxl);
}
</script>
<script language="JavaScript">
function formCheck()
{
if(!isIdno(document.testForm.id.value)){
alert("身份证号码输入有误!要求只能输入数字,长度为16或18位。");
document.testForm.id.focus();
return false;}
if(!isChinaTel(document.testForm.tel.value)){
alert("固定电话号码输入有误!合法的格式为:区号-电话号码。");
document.testForm.tel.focus();
return false;}
if(!isChinaMob(document.testForm.mobi.value)){
alert("移动电话号码输入有误!");
document.testForm.mobi.focus();
return false;}
if(!isMail(document.testForm.mail.value)){
alert("邮箱输入有误!");
document.testForm.mail.focus();
return false;}
if(!isPostCode(document.testForm.post.value)){
alert("邮政编码输入有误!要求只能输入数字,长度为6位。");
document.testForm.post.focus();
return false;}
if(!isNLU(document.testForm.nlu.value)){
alert("用户注册 该域只能输入数字、字母或下划线!");
document.testForm.nlu.focus();
return false;}
var len=getLength(document.testForm.leng.value);
if(len<4||len>6){
alert("该域要求输入长度范围是4至6个字符!");
document.testForm.leng.focus();
return false;}
}
</script>
<form name=testForm onsubmit="return formCheck();" method=post>
身份证号码:<br>
<input name=id type="text">
<br>固定电话: <br>
<input name=tel type="text">
<br>手机号码:<br>
<input name=mobi type="text">
<br>邮箱:<br>
<input name=mail type="text">
<br>邮编:<br>
<input name=post type="text">
<br>用户注册 只能输入数字、字母、下划线:<br>
<input name=nlu type="text">
<br>输入长度在4-6之间:<br>
<input name=leng type="text">
<br><br>
<input type=submit value="点击测试" name=Submit>
</form>
===================================================================================
<form name="Form1" method="post" action="unionreg.aspx?unionid=31388&unionkey=" id="Form1" onSubmit="javascript:return CheckInput();">
<script language="javascript">
var surl = escape(document.referrer);
var a = location.href.indexOf("?");
var otherkey;
if(a>0)
{
otherkey = "&" + location.href.substring(a+1,location.href.length);
}
else
{
otherkey = "";
}
var url = "http://u1.kubao.com/countad.aspx?surl="+ surl +"&topurl=" + escape(location.href) + otherkey;
url = "<IFRAME id='getCount' name='getCount' border='0' frameBorder='0' width='0' scrolling='no' height='0' src='" + url +"'></IFRAME>"
document.write(url);
</script>
<table width="779" border="0" align="center" cellpadding="0" cellspacing="0">
</table>
<table width="207" height="330" border="0" cellpadding="0" cellspacing="0">
<tr>
<td nowrap class="kb_wd1">用户名:</td>
<td><input name="Txt_UserName" type="text" maxlength="18" id="Txt_UserName" tabindex="1" onBlur="j_username()" /></td>
</tr>
<tr>
<td colspan="2" class="kb_wd2"><span id="NameError" class="kb_wd2">※ 用户名只能是字母、数字、下划<br>
线 (_) 或其组合,并且以字母为开头<br>
长度为5~18个字符。</span></td>
</tr>
<tr>
<td nowrap class="kb_wd1">登录密码:</td>
<td><input name="Txt_Password" type="password" maxlength="12" id="Txt_Password" tabindex="2" onBlur="j_password()" /></td>
</tr>
<tr>
<td colspan="2" class="kb_wd2"><span id="PwdError" class="kb_wd2">※ 密码只能是6~12位字母、数字或<br>
二者组合,区分大小写。</span></td>
</tr>
<tr>
<td class="kb_wd1">确认密码:</td>
<td><input name="Txt_Password_AG" type="password" maxlength="12" id="Txt_Password_AG" tabindex="3" onBlur="j_password1()" /></td>
</tr>
<tr>
<td colspan="2"><span id="PwdError2"></span></td>
</tr>
<tr>
<td class="kb_wd1">邮件地址:</td>
<td><input name="Txt_Email" type="text" maxlength="30" id="Txt_Email" tabindex="4" onBlur="j_email()" /></td>
</tr>
<tr>
<td colspan="2"><span id="EmailError"></span></td>
</tr>
<tr>
<td colspan="2"><span id="AgreeError"></span></td>
</tr>
<tr>
<td class="kb_wd1">性 别:</td>
<td><table id="List_Sex" border="0">
<tr>
<td><input id="List_Sex_0" type="radio" name="List_Sex" value="True" tabindex="5" />
<label for="List_Sex_0">帅哥</label></td>
<td><input id="List_Sex_1" type="radio" name="List_Sex" value="False" checked="checked" tabindex="5" />
<label for="List_Sex_1">靓妹</label></td>
</tr>
</table></td>
</tr>
<tr align="center">
<td height="27" colspan="2" class="kb_wd2"><input id="CB_Agree" type="checkbox" name="CB_Agree" checked="checked" />
<a target="_blank" href="http://login.kubao.com/agreement.htm">我已阅读并同意酷宝使用协议</a></td>
</tr>
<tr align="center">
<td colspan="2"><label>
<input type="submit" name="Submit" value="提交">
</label></td>
</tr>
</table>
</form>
<script src="http://login.kubao.com/js/guid.js"></script>
<script language="javascript">
var a = location.href.indexOf("?");
var otherkey;
if(a>0)
{
otherkey = location.href.substring(a+1,location.href.length);
}
else
{ otherkey = "";}
document.write(otherkey);
//document.getElementById("divActiveDown").innerHTML = "<a href=\"http://union.kubao.com/downloadIM.aspx?" + otherkey + "\"><img src=\"images/ball.gif\" width=\"144\"
height=\"152\" border=\"0\"></a>";
</script>
<script language="javascript">
<!--
window.Form1.Txt_UserName.focus();
var xmlDom = false;
var arr_UserName = new Array();
var arr_BackInfo = new Array();
var canSubmit = false;
try
{
xmlDom = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (E)
{
try
{
xmlDom = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
}
}
function j_password()
{
var password = document.getElementById("Txt_Password").value;
if (password.length < 6 || password.length > 12)
{
document.getElementById("PwdError").innerHTML = "<font color=\"red\">密码长度不正确。<font>";
canSubmit = false;
return;}
if (document.getElementById("Txt_UserName").value == password)
{ document.getElementById("PwdError").innerHTML = "<font color=\"red\">为了您的安全,用户名与密码不能一致,请使用新的密码<font>";
canSubmit = false;
return;
}
var reg = "";
if(password.match(reg))
{
document.getElementById("PwdError").innerHTML = "<font color=\"red\">密码只能是英文或者数字!<font>";
canSubmit = false;
return;
}
canSubmit = true;
document.getElementById("PwdError").innerHTML = "";
}
function j_password1()
{
var password = document.getElementById("Txt_Password_AG").value;
if(password != document.getElementById("Txt_Password").value)
{
document.getElementById("PwdError2").innerHTML = "<font color=\"red\">两次输入的密码不一致,请重新输入!<font>";
document.getElementById("Txt_Password_AG").value = "";
canSubmit = false;
return;
}
document.getElementById("PwdError2").innerHTML = "";
canSubmit = true;
}
function j_email()
{
var email = document.getElementById("Txt_Email").value;
var reg = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/;
if(!email.match(reg))
{
document.getElementById("EmailError").innerHTML = "<font color=\"red\">邮件地址无效!<font>";
canSubmit = false;
return;
}
document.getElementById("EmailError").innerHTML = "";
canSubmit = true;
}
function j_submit()
{
//再调一次js判断,避免用户不输入密码和邮件提交不触发时间导致无效的注册请求
//反正用户机器速度快 ~_~
var submitOK = true;
canSubmit = true;
j_username();
submitOK = submitOK & canSubmit;
j_password();
submitOK = submitOK & canSubmit;
j_password1();
submitOK = submitOK & canSubmit;
j_email();
submitOK = submitOK & canSubmit;
checkAgreed();
submitOK = submitOK & canSubmit;
return submitOK;
}
function j_username()
{
document.getElementById("NameError").innerHTML = "";
var userName = document.getElementById("Txt_UserName").value;
var reg = "[^a-zA-Z_0-9]";
var reg1 = "";
if(userName == "")
{
document.getElementById("NameError").innerHTML = "<font color=\"red\">用户名不能为空!<font><br>";
canSubmit = false;
}
else if(userName.length < 5)
{
document.getElementById("NameError").innerHTML = "<font color=\"red\">你输入的用户名长度太短!长度必须为5~18位!<font><br>";
canSubmit = false;
}
else
{
if(!userName.substring(0,1).match(reg1))
{
document.getElementById("NameError").innerHTML = "<font color=\"red\">必须已字母开头!<font><br>";
canSubmit = false;
}
else if(userName.match(reg))
{
document.getElementById("NameError").innerHTML = "<font color=\"red\">用户名只能是字母、数字、下划线!<font><br>";
canSubmit = false;
}
else
{
if(typeof(xmlDom) != 'undefined')
{
var isChecked = false;
//先检测是否用户已经查询过用户是否存在
for(var i=0;i<arr_UserName.length;i++)
{
if(document.getElementById("Txt_UserName").value == arr_UserName[i])
{
switch (arr_BackInfo[i])
{
case "0":
document.getElementById("NameError").innerHTML = "<font color=\"red\">该用户名不符合注册规则,您可以尝试注册!<font><br>";
canSubmit = false;
break;
case "1":
document.getElementById("NameError").innerHTML = "<font color=\"red\">对不起,您注册的用户名已经存在!<font><br>";
canSubmit = false;
break;
case "2":
document.getElementById("NameError").innerHTML = "<font color=\"red\">恭喜您,该用户名无人使用!<font><br>";
canSubmit = true;
break;
}
isChecked = true;
break;
}
}
if(!isChecked)
{
checkname(document.getElementById("Txt_UserName").value);
}
}
}
}
}
function checkAgreed()
{
document.getElementById("AgreeError").innerHTML = "";
if(document.getElementById("CB_Agree").checked.toString().toLowerCase()=="false")
{
document.getElementById("AgreeError").innerHTML = "<font color=\"red\">您接受酷宝使用协议后才能继续注册。<font>";
canSubmit = false;
return;
}
canSubmit = true;
}
function checkname(name)
{
var d = new Date();
var strURL = "checkUserName.aspx?username=" + name + "&guid=" + d.getTime();
xmlDom.Open("get",strURL , true);
xmlDom.onreadystatechange = fnRun;
xmlDom.Send(xmlDom);
document.getElementById("NameError").innerHTML = "<font color=\"red\">正在检测用户名是否重复...您可以尝试注册!<font><br>";
//canSubmit = true;
}
function fnRun()
{
if (xmlDom.readyState == 4 && xmlDom.status == 200)
{
var str = xmlDom.responseText;
//保存已经查询过的用户名,下次直接返回
arr_UserName[arr_UserName.length] = document.getElementById("Txt_UserName").value;
arr_BackInfo[arr_BackInfo.length] = str;
switch (str)
{
case "0":
document.getElementById("NameError").innerHTML = "<font color=\"red\">该用户名不符合注册规则,您可以尝试注册!<font><br>";
canSubmit = false;
break;
case "1":
document.getElementById("NameError").innerHTML = "<font color=\"red\">对不起,您注册的用户名已经存在!<font><br>";
canSubmit = false;
break;
case "2":
document.getElementById("NameError").innerHTML = "<font color=\"red\">恭喜您,该用户名无人使用!<font><br>";
canSubmit = true;
break;
}
}
}
function CheckInput()
{
//然后再依据各项收入来判断是否有错误
//document.getElementById("Reguser1_Lab_Info").innerHTML = "";
if (!j_submit())
{
alert("对不起,由于您的注册信息有误,您还不能提交。\r\n请根据页面上的错误提示,检查您的注册信息。");
//document.getElementById("Reguser1_Lab_Info").innerHTML = "对不起,由于您的注册信息有误,您还不能提交。请根据页面上的错误提示,检查您的注册信息。";
return false;
}
return true;
}
function ClearCache()
{
arr_UserName = new Array();
arr_BackInfo = new Array();
}
//-->
</script>
posted on 2012-03-26 14:55 zhangtao1212 阅读(3122) 评论(0) 编辑 收藏 举报