1、正则表达式验证密码账号等:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Regular Expression test</title>
</head>
<body>
用户名:(4-16位,字母、下划线、数字,减号) <br/>
<input type="text" id="un" placeholder="4-16位" >
<input type="button" value="test" onclick="userName()" >
<span id="one"></span>
<br/><hr/>
密码:(最少6位,至少包括一个大写字母、一个小写字母、一个数字、一个特殊字符) <br/>
<input type="text" id="pw" >
<input type="button" value="test" onclick="password()" >
<span id="two"></span>
<br/><hr/>
邮箱:([email protected])<br/>
<input type="text" id="em" >
<input type="button" value="test" onclick="email()" >
<span id="three"></span>
<br/><hr/>
身份证号:18位,最后一位可以是数字或字母<br/>
<input type="text" id="idn" >
<input type="button" value="test" onclick="idNumber()" >
<span id="four"></span>
<script type="text/javascript">
function userName(){
var reg=/^\w{4,16}$/;
if( !reg.test( document.getElementById("un").value ) )
document.getElementById("one").innerHTML="不正确";
else
document.getElementById("one").innerHTML="正确";
}
function password(){
var reg=/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*?[#[email protected]$%^&*-]).{6,}$/;
if( !reg.test( document.getElementById("pw").value ) )
document.getElementById("two").innerHTML="不正确";
else
document.getElementById("two").innerHTML="正确";
}
function email(){
var reg=/^\[email protected][a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
if( !reg.test( document.getElementById("em").value ) )
document.getElementById("three").innerHTML="不正确";
else
document.getElementById("three").innerHTML="正确";
}
function idNumber(){
var reg=/^\d{17}[A-z|0-9]{1}$/;
if( !reg.test( document.getElementById("idn").value ) )
document.getElementById("four").innerHTML="不正确";
else
document.getElementById("four").innerHTML="正确";
}
</script>
</body>
</html>
效果图如下
原文地址:https://www.cnblogs.com/yohoooooo/p/10198408.html