日期:2014-05-16  浏览次数:20321 次

js表单验证
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>welcome</title>
<script language="javascript" src="jonny.js">
</script>
</head>
<body>
<form  action="#"     >
<p><label >
userName:
<input type="text" id="userName"/>
</label></p>
<p><label>
password:
<input type="text" id="password1"/>
</label></p>
<p><label>
confirm password:
<input type="text" id="password2"/>
</label></p>
<p><input type="checkbox">I  AGREE  IT</p>
<input  type="submit"  value="logon" onclick="return check()"/><input type="reset" value="reseet">
</form>
</body>
</html>





//<script language="JavaScript">
function check(){
var user=document.getElementById("userName").value;
var pass=document.getElementById("password1").value;
var rpass=document.getElementById("password2").value;
if(user.match("([^a-zA-Z0-9_]+)")!=null || user.length<3||user.length>15){
alert("您输入的用户名应由字母数字下划线组成,长度在3-15位之间!");
return false;
}
else if((pass.length>20||pass.length<6)||(pass==user)){
alert("您的密码长度过短或者密码与姓名相同,请重新输入!");
return false;
}
else if(pass!=rpass){
alert("您两次输入的密码不一致");
return false;
}
else{
alert("登录成功!");
}

}
//</script>