<!--
function check()
{
if(document.form.email.value.length!=0)
{
if (document.form.email.value.charAt(0)=="." ||
document.form.email.value.charAt(0)=="@"||
document.form.email.value.indexOf('@', 0) == -1 ||
document.form.email.value.indexOf('.', 0) == -1 ||
document.form.email.value.lastIndexOf("@")==document.form.email.value.length-1 ||
document.form.email.value.lastIndexOf(".")==document.form.email.value.length-1)
{
alert("Email的格式不正确!")
document.form.email.focus()
return false
}
}
else
{
alert("Email的不能为空!")
document.form.email.focus()
return false
}
}
//-->
</SCRIPT>
<div id="apDiv2"><img src="image/天蓝色主色调.JPG" width="956" height="35" /></div>
<div id="apDiv3">
<form id="form" name="form1" method="get" action="checkuser.asp" onsubmit="return check()">
这段代码可以用,已测试!希望对你有帮助
<html><head>
<script type="text/javascript">
function validate()
{
var ok=true,un,pw1,pw2,el,ch0
var r = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g
if(""==(un=document.f.user.value))
{
alert("用户名不能为空!")
document.f.user.focus()
ok=false
}
else if(""==(pw1=document.f.password1.value))
{
alert("密码不能为空!")
document.f.password1.focus()
ok=false
}
else if(""==(pw2=document.f.password2.value))
{
alert("重复密码不能为空!")
document.f.password2.focus()
ok=false
}
else if(""==(el=document.f.email.value))
{
alert("邮箱不能为空!")
document.f.email.focus()
ok=false
}
if(ok)
{
if(!(un.length>=6 &&un.length<=20))
{
alert("用户名长度必须大于等于6小于等于20!")
document.f.user.focus()
ok=false
}
else
{
ch0=un.charAt(0).toLowerCase()
if(!(ch0>="a" &&ch0<="z"))
{
alert("用户名必须以字母开头!")
document.f.user.focus()
ok=false
}
else if(pw1!=pw2)
{
alert("重复密码与密码必须相同!")
document.f.password2.focus()
ok=false
}
else if(!r.test(el))
{
alert("不是有效的邮箱地址!")
document.f.email.focus()
ok=false
}
}
}
if(ok)
{
alert("恭喜你,通过了有效性验证!")
}
}
</script>
</head>
<body>
<form name="f">
用户名:<input type="text" name="user" /><br /><br />
密码:<input type="text" name="password1" /><br /><br />
重复密码:<input type="text" name="password2" /><br /><br />
邮箱:<input type="text" name="email" /><br /><br />
<input type="button" value="验证" onclick="validate()" />
</form>
</body>
</html>
用正则判断,例子如下:function isEmail(strEmail) {
if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1){
return true
}else{
return false
}
图上的效果那就是样式的问题了
格式错误时,输入框获取光标 xxx.focus(),改变输入框颜色与背景色,右边的提示是个层,默认为隐藏,错误时显示