jQuery表单验证正则表达式-简单

时间:2024-03-02 12:04:23
     
     <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title></title>
      
     <script type ="text/javascript">
     //用户名:
     function names() {
     var name = document.getElementById("name").value;
     var regrx = /^[0-9a-zA-Z][0-9a-zA-Z_.-]{2,16}[0-9a-zA-Z]$/;
     if (!regrx.test(name)) {
     document.getElementById("span1").style.color = "#f00";
     document.getElementById("span1").innerHTML = "请您填写由字母,数字,下划线,点,减号组成的4-18位字符,以数字,字母开头或者结尾!";
      
     return false;
     }
     document.getElementById("span1").innerHTML = "填写正确!";
     document.getElementById("span1").style.color = "#000";
     return true;
     }
     //昵称
     function usenames() {
     var regx = /^([\u4e00-\u9fa5]|\w)+$/;
     var uesname = document.getElementById("uesname").value;
      
     var regx2 = /[\u4e00-\u9fa5]/g;
     var six = uesname.replace(regx2, "aa").length;
      
     if (!regx.test(uesname)) {
     document.getElementById("span2").style.color = "#f00";
     document.getElementById("span2").innerHTML = "请您填写包含汉字,字母,数字,下划线的4-20位字符,汉字占两个字符!";
     return false;
     }
     if (six<4||six>20) {
     document.getElementById("span2").style.color = "#f00";
     document.getElementById("span2").innerHTML = "请您填写包含汉字,字母,数字,下划线的4-20位字符,汉字占两个字符!";
     return false;
     }
      
     document.getElementById("span2").innerHTML = "填写正确!";
     document.getElementById("span2").style.color = "#000";
     return true;
      
     }
     //邮箱
     function mails() {
     var mail = document.getElementById("mail").value;
     var regx = /^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;
      
     if (!regx.test(mail)) {
     document.getElementById("span3").style.color = "#f00";
     document.getElementById("span3").innerHTML = "请您填写邮箱域名:@域名,如good@tom.com,win@sina.com.cn";
     return false;
     }
     document.getElementById("span3").innerHTML = "填写正确!";
     document.getElementById("span3").style.color = "#000";
     return true;
      
     }
     //密码
     function pwds() {
     var pwd = document.getElementById("pwd").value;
     var regrx = /^[a-zA-Z0-9]{4,10}$/;
      
     if (!regrx.test(pwd)) {
     document.getElementById("span4").style.color = "#f00";
     document.getElementById("span4").innerHTML = "请您填写由英文字母和数字所组成的4-10位字符!";
      
     return false;
     }
     document.getElementById("span4").innerHTML = "填写正确!";
     document.getElementById("span4").style.color = "#000";
     return true;
     }
     //确认密码
     function dearpwds() {
     var dearpwd = document.getElementById("dearpwd").value;
     var pwd = document.getElementById("pwd").value;
     if (pwd!=dearpwd) {
     document.getElementById("span5").style.color = "#f00";
     document.getElementById("span5").innerHTML = "您输入的密码不一致,请确认是否一致!";
      
     return false;
     }
     if (dearpwd.trim()=="") {
      
     document.getElementById("span5").innerHTML = "不能为空!"; return false;
     }
     document.getElementById("span5").innerHTML = "填写正确!";
     document.getElementById("span5").style.color = "#000";
     return true;
      
      
     }
     //手机号码
     function phones() {
     var phone = document.getElementById("phone").value;
     var reg = /^(13|15|18)\d{9}$/;
     if (!reg.test(phone)) {
     document.getElementById("span6").style.color = "#f00";
     document.getElementById("span6").innerHTML = "请您填写手机号11位数字,且以13,15,18开头!";
      
     return false;
     }
     document.getElementById("span6").innerHTML = "填写正确!";
     document.getElementById("span6").style.color = "#000";
     return true;
     }
     //身份证
      
     function idcards() {
     var idcard = document.getElementById("idcard").value;
      
     var regx = /^\d{15}$|^\d{18}$/;
     if (!regx.test(idcard)) {
     document.getElementById("span7").style.color = "#f00";
     document.getElementById("span7").innerHTML = "请您填写由15位数字或者18位数字组成!";
      
     return false;
     }
     document.getElementById("span7").innerHTML = "填写正确!";
     document.getElementById("span7").style.color = "#000";
     return true;
      
     }
      
     function butts() {
     if (names() && phones() && idcards() && dearpwds() && pwds() && mails() && usenames()) {
     return true;
     }
     document.getElementById("span8").style.display = "inline";
     return false;
      
      
     }
     </script>
      
     </head>
     <body>
      
     <form action="HtmlPage.html" method="post" onsubmit="return butts()">
     <fieldset>
     <legend>请填写用户信息</legend>
     <p>用户名:
     <input type="text" id="name" onblur="names()" />
     <span id="span1" style="color:#ccc">由字母,数字,下划线,点,减号组成的4-18位字符,以数字,字母开头或者结尾!</span>
     </p>
     <p>昵称:
     <input type="text" id="uesname" onblur="usenames()" />
     <span id="span2" style="color:#ccc">包含汉字,字母,数字,下划线的4-20位字符,汉字占两个字符</span>
     </p>
     <p>邮箱:
     <input type="text" id="mail" onblur="mails()" />
     <span id="span3" style="color:#ccc">邮箱域名@域名,如good@tom.com,win@sina.com.cn</span>
     </p>
     <p>密码:
     <input type="text" id="pwd" onblur="pwds()"/>
     <span id="span4" style="color:#ccc">由英文字母和数字所组成的4-10位字符</span>
     </p>
     <p>确认密码:
     <input type="text" id="dearpwd" onblur="dearpwds()"/>
     <span id="span5" style="color:#f00"></span>
     </p>
      
     <p>手机号码:
     <input type="text" id="phone" onblur="phones() "/>
     <span id="span6" style="color:#ccc"> 手机号11位数字,且以13,15,18开头</span>
     </p>
      
     <p>身份证:
     <input type="text" id="idcard" onblur="idcards()" />
     <span id="span7" style="color:#ccc">由15位数字或者18位数字组成</span>
     </p>
     <p>
     <input type="submit" id="submits" />
     <span id="span8" style="color:#f00;display:none;" >您好像有地方填错了哦!</span>
     </p>
     </fieldset>
      
      
      
      
      
     </form>
     </body>
     </html>