1 用户名正则
//用户名正则,4到16位(字母,数字,下划线,减号) var uPattern = /^[a-zA-Z0-9_-]{4,16}$/; //输出 true console.log(uPattern.test("caibaojian"));
2 Email正则
//Email正则 var ePattern = /^([A-Za-z0-9_-.])+@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/; //输出 true console.log(ePattern.test("99154507@qq.com"));
3 密码强度正则
//密码强度正则,最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符 var pPattern = /^.*(?=.{6,})(?=.*d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/; //输出 true console.log("=="+pPattern.test("caibaojian#"));
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
fChkMail=function(szMail){
var szReg=/^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/; var bChk=szReg.test(szMail);
return bChk;
}
</script>
<script type="text/javascript">
fChkMail = function(username){
var uPattern = /^[a-zA-Z0-9_-]{4,16}$/;
console.log(uPattern.test("caibaojian"));
}
</script>
<script type="text/javascript">
fChkMail = function(password){
var pPattern = /^.*(?=.{6,})(?=.*d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
console.log("=="+pPattern.test("caibaojian#"));
}
</script>
<h3>登录<br></h3>
<input type="text" id="username" value=""/>
<input type="button" value="用户名" onclick="alert(fChkMail(document.getElementById('username').value));"><br><br>
<input type="text" id="Mail" value="" />
<input type="button" value="验证邮箱地址" onclick="alert(fChkMail(document.getElementById('Mail').value));"/> <br><br>
<input type="text" id="password" value=""/>
<input type="button" value="密码" onclick="alert(fChkMail(document.getElementById('password').value));"><br>
</body>
</html>