来看看其他验证是不是很简单啦!
<2>昵称验证
Js代码
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代码
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代码
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();
}
怎么样,挺简单的吧!(未完待续)
本文介绍了一种使用JavaScript进行前端表单验证的方法,包括对昵称、密码及其确认密码的即时反馈验证,确保用户输入符合规范。
287

被折叠的 条评论
为什么被折叠?



