正则表达式:以^开始,$结束
/验证密码是620位之间的az、A~Z 0-9
//验证密码是6~20位之间的a~z、A~Z 0-9
// var reg = new RegExp(/^[\w*&%$#_@]{6,20}$/);
//身份证15 18 17xX
// var reg = new RegExp(/^\d{15}$|^\d{18}$|^\d{17}[x|X]{1}$/);
//英文数字._ @ 英文数字 . 英文数字
var reg = new RegExp(/^([\w._]+)@(\w+)\.(\w+)$/);
if(reg.test("12345678901234567x")){
alert("正确");
}else{
alert("错误");
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function validate(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var repassword = document.getElementById("repassword").value;
var userid = document.getElementById("userid").value;
var email = document.getElementById("email").value;
// alert(username);
var reg = new RegExp(/^\w{6,20}$/);
if(!reg.test(username)){
alert("用户名错误");
return false;
}
var pwReg = new RegExp(/^[\w_]{6,20}$/);
if(!pwReg.test(password)){
alert("密码输入错误");
return false;
}
if(password!=repassword){
alert("两次密码不一致");
return false;
}
return true;
}
</script>
</head>
<body>
<form action="#" id="form1" onsubmit="return validate();">
用户名:<input type="text" name="username" id="username" /><br>
密码:<input type="password" name="password" id="password" /><br>
重复密码:<input type="password" name="password" id="repassword" /><br>
身份证号码:<input type="text" name="userid" id="userid" /><br>
邮箱:<input type="text" name="email" id="email" /><br>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
</body>
</html>
网友评论