來看看其他驗證是不是很簡單啦!
<2>昵稱驗證
Js代碼
[javascript]
function checkNickname(Nickname)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("error2").innerHTML="<font color=red size=2px>*</font>"; //復位
if(Nickname.length==0)
{
document.getElementById("error2").innerHTML="<font color=red size=2px>*昵稱不能為空!</font>";
}
else
{
if(Nickname.length>16)
{
document.getElementById("error2").innerHTML="<font color=red size=2px>*昵稱不要超過16個字符!</font>";
}
else
{
document.getElementById("error2").innerHTML="<font color=green size=2px>*昵稱可用!</font>";
}
}
}
}
xmlhttp.open("GET","index.php",true);
xmlhttp.send(); //注意這裡與郵箱驗證的不同
}
function checkNickname(Nickname)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("error2").innerHTML="<font color=red size=2px>*</font>"; //復位
if(Nickname.length==0)
{
document.getElementById("error2").innerHTML="<font color=red size=2px>*昵稱不能為空!</font>";
}
else
{
if(Nickname.length>16)
{
document.getElementById("error2").innerHTML="<font color=red size=2px>*昵稱不要超過16個字符!</font>";
}
else
{
document.getElementById("error2").innerHTML="<font color=green size=2px>*昵稱可用!</font>";
}
}
}
}
xmlhttp.open("GET","index.php",true);
xmlhttp.send(); //注意這裡與郵箱驗證的不同
}
<3>密碼驗證
Js代碼
[javascript]
function checkPwd1(password1)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ document.getElementById("error3").innerHTML="<font color=red size=2px>*</font>";
document.getElementById("password2").value="";
document.getElementById("error4").innerHTML="<font color=red size=2px>*</font>";
if(password1.length==0)
{
document.getElementById("error3").innerHTML="<font color=red size=2px>*密碼不能為空!</font>";
}
else
{
if(password1.length<6||password1.length>16)
{
document.getElementById("error3").innerHTML="<font color=red size=2px>*密碼為6-16個字符!</font>";
}
else
{
var reg=/[a-zA-Z0-9]/; //在js中使用正則表達式 www.2cto.com
if(reg.test(password1))
{
document.getElementById("error3").innerHTML="<font color=green size=2px>*密碼可用!</font>";
}
else
{
document.getElementById("error3").innerHTML="<font color=red size=2px>*密碼不可用!</font>";
}
}
}
}
}
xmlhttp.open("GET","index.php",true);
xmlhttp.send();
}
function checkPwd1(password1)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ document.getElementById("error3").innerHTML="<font color=red size=2px>*</font>";
document.getElementById("password2").value="";
document.getElementById("error4").innerHTML="<font color=red size=2px>*</font>";
if(password1.length==0)
{
document.getElementById("error3").innerHTML="<font color=red size=2px>*密碼不能為空!</font>";
}
else
{
if(password1.length<6||password1.length>16)
{
document.getElementById("error3").innerHTML="<font color=red size=2px>*密碼為6-16個字符!</font>";
}
else
{
var reg=/[a-zA-Z0-9]/; //在js中使用正則表達式
if(reg.test(password1))
{
document.getElementById("error3").innerHTML="<font color=green size=2px>*密碼可用!</font>";
}
else
{
document.getElementById("error3").innerHTML="<font color=red size=2px>*密碼不可用!</font>";
}
}
}
}
}
xmlhttp.open("GET","index.php",true);
xmlhttp.send();
}
<4>重復密碼驗證
Js代碼
[javascript]
function checkPwd2(password2)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ document.getElementById("error4").innerHTML="<font color=red size=2px>*</font>";
if(password2.length==0)
{
document.getElementById("error4").innerHTML="<font color=red size=2px>*請確認密碼!</font>";
}
else
{
if(password2!=document.getElementById("password1").value)
{
document.getElementById("error4").innerHTML="<font color=red size=2px>*兩次密碼輸入不一致!</font>";
}
else
{
document.getElementById("error4").innerHTML="<font color=green size=2px>*密碼輸入一致!</font>";
}
}
}
}
xmlhttp.open("GET","index.php",true);
xmlhttp.send();
}
function checkPwd2(password2)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ document.getElementById("error4").innerHTML="<font color=red size=2px>*</font>";
if(password2.length==0)
{
document.getElementById("error4").innerHTML="<font color=red size=2px>*請確認密碼!</font>";
}
else
{
if(password2!=document.getElementById("password1").value)
{
document.getElementById("error4").innerHTML="<font color=red size=2px>*兩次密碼輸入不一致!</font>";
}
else
{
document.getElementById("error4").innerHTML="<font color=green size=2px>*密碼輸入一致!</font>";
}
}
}
}
xmlhttp.open("GET","index.php",true);
xmlhttp.send();
}
怎麼樣,挺簡單的吧!(未完待續)
摘自 wyzhangchengjin123