var str = "a1_"
console.log(reg.test(str))
// 将心比心,采纳即可
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form>
<input type="text" name="fname" id="fname"/>
<input type="button" value="Submit" id="submit"/>
</form>
<script>
window.onload=function(){
/*js 求正则表达式 : 8-20位 必须包涵大写字母,小写字母,数字,特殊字符, 必须包涵哈.*/
document.getElementById("submit").onclick=function(){
var fname=document.getElementById("fname").value
var pattern=/[A-Z]+[a-z]+[0-9]+[^A-Za-z0-9]+/g
alert(pattern.test(fname))
if(fname.length<8 || fname.length>20){
alert("长度必须是8-20位")
}
}
}
</script>
</body>
</html>