<!DOCTYPE html>
<html> <head> <title>test</title> </head> <script type="text/javascript"> function check(){ var value = document.getElementById("pwd").value; var reg=new RegExp('^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$');//位数自行调整 if(reg.test(value)){ alert("完全正确") return true; }else{ alert("密码不合法") return false; } } </script> <body> <input type="text" name="password" id="pwd"> <button onClick=check()>Test</button> </body> </html>