正则表达式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>正则表达</title> </head> <body> <div align="center"> <fieldset style="height:30%;width:30%;"> <legend align="center">javascript之正则表达</legend> 用户名:<input type="text" name="usme" id="usme" placeholder="4到16位(字母,数字,下划线,减号"> <input type="button" value="提交" onclick="usme()"><br> 身份证号:<input type="text" name="IDcard" id="IDcard" placeholder="请按标准形式输入"> <input type="button" value="提交" onclick="IDcard()"><br> Email:<input type="text" name="Email" id="Email" placeholder="邮箱地址"> <input type="button" value="提交" onclick="Email()"><br> 密码:<input type="text" name="password" id="password" placeholder="请输入密码"> <input type="button" value="提交" onclick="password()"> </fieldset> </div> <script> function usme() { var usme=document.getElementById("usme").value; var UsME=/^[a-zA-Z0-9_-]{4,16}$/; if(!UsME.test(usme)) { alert("提交成功"); } else { alert("用户名格式错误"); } } function IDcard() { var IDcard=document.getElementById("IDcard").value; var IDCard=/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/; if(IDCard.test(IDcard)) { alert("提交成功"); } else { alert("身份证号格式错误"); } } function Email() { var Email=document.getElementById("Email").value; var EMAIL=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; if(EMAIL.test(Email)) { alert("提交成功"); } else { alert("邮箱地址格式错误"); } } function password() { var password=document.getElementById("password").value; var Password=/^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/; if(Password.test(password)) { alert("提交成功"); } else { alert("密码格式错误"); } } </script> </body> </html>