当前位置:   article > 正文

正则表达式转为javascript脚本_正则表达式转换脚本

正则表达式转换脚本

function validateReg()
        var username = document.getElementById("username");
              //用户名称的验证
              var name = /^[a-z0-9_-]{3,16}$/;
              if(!name.test(username.value)){
                      alert("提示\n\n用户名,请输入【3-16】数字或字母");
                      username.focus();
                      return false;
              }
               var ps = document.getElementById("pwd");
              //密码验证
              var pwdreg = /^[a-z0-9_-]{6,18}$/;
              if(!pwdreg.test(ps.value)){
                      alert("提示\n\n密码,请输入【6-18】数字或字母");
                      pwdreg.focus();
                      return false;
              }
                                 var temp = document.getElementById("userTrueName");
              //对电子邮件的验证
              var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
              if(!myreg.test(temp.value))
              {
                   alert('提示\n\n请输入有效的E_mail!');
                   myreg.focus();
                   return false;
              }
                                  var url = document.getElementById("url");
              //url校验
              var urlreg = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
              if(!urlreg.test(url.value)){
                      alert("提示\n\n请输入正确的url");
                      urlreg.focus();
                      return false;
              }
                                   var ip = document.getElementById("ip");
              //IP校验
              var ipreg = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;

              if(!ipreg.test(ip.value)){
                      alert("提示\n\n请输入正确的Ip");
                      ipreg.focus();
                      return false;
              }
                                    var html = document.getElementById("html");
              //IP校验
              var htmlreg = /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/;

              if(!htmlreg.test(html.value)){
                      alert("提示\n\n请输入正确的html标签");
                      htmlreg.focus();
                      return false;
              }
                                   var hz = document.getElementById("hz");
              //中文校验
              var hzreg =/[\u4e00-\u9fa5]/;

              if(!hzreg.test(hz.value)){
                      alert("提示\n\n请输入中文");
                      hzreg.focus();
                      return false;
              }
                                    var szj = document.getElementById("szj");
              //双字节字符校验
              var szjreg =/[^\x00-\xff]/;

              if(!szjreg.test(hz.value)){
                      alert("提示\n\n请输入双字节");
                      szjreg.focus();
                      return false;
              }
                                   var kg = document.getElementById("kg");
              //空格校验
              var kgreg =/\n\s*\r/;

              if(!kgreg.test(kg.value)){
                      alert("提示\n\n没有空格");
                      kgreg.focus();
                      return false;
                                  }
                                    var headtail = document.getElementById("headtail");
              //空格校验
              var headtailreg =/^\s*|\s*$/;

              if(!headtailreg.test(headtail.value)){
                      alert("提示\n\n行头行尾没有空格");
                      headtailreg.focus();
                      return false;
              }
                                     var tel = document.getElementById("tel");
              //电话号码校验
              var telreg =/\d{3}-\d{8}|\d{4}-\d{7}/;

              if(!telreg.test(tel.value)){
                      alert("提示\n\n请输入正确的电话号码");
                      telreg.focus();
                      return false;
              }
                                   var qq = document.getElementById("qq");
              //qq号码校验
              var qqreg =/[1-9][0-9]{4,}/;

              if(!qqreg.test(qq.value)){
                      alert("提示\n\n请输入正确的QQ号码");
                      qqreg.focus();
                      return false;
              }
                                   var postcode = document.getElementById("postcode");
              //邮政编码校验
              var postcodereg =/[1-9]\d{5}(?!\d)/;

              if(!postcodereg.test(postcode.value)){
                      alert("提示\n\n请输入正确的邮政编码");
                      postcodereg.focus();
                      return false;
              }
                                      var idCard = document.getElementById("idCard");
              //身份证号校验
              var idCardreg =/\d{15}|\d{18}/;

              if(!idCardreg.test(idCard.value)){
                      alert("提示\n\n请输入正确的身份证号");
                      idCardreg.focus();
                      return false;
              }
                                     var phone = document.getElementById("phone");
              //手机号校验
              var phonereg =/^1[3|4|5|8][0-9]\d{4,8}$/;

              if(!phonereg.test(phone.value)){
                      alert("提示\n\n请输入正确的手机号");
                      phonereg.focus();
                      return false;
              }
                                   var zs = document.getElementById("zs");
              //正整数校验
              var zsreg =/^[1-9]\d*$/;

              if(!zsreg.test(zs.value)){
                      alert("提示\n\n请输入正整数");
                      zsreg.focus();
                      return false;
              }
                                   var fzs = document.getElementById("fzs");
              //负整数校验
              var fzsreg =/^-[1-9]\d*$/;

              if(!fzsreg.test(fzs.value)){
                      alert("提示\n\n请输入负整数");
                      fzsreg.focus();
                      return false;
              }
                                   var inter = document.getElementById("inter");
              //整数校验
              var interreg =/^-?[1-9]\d*$/;

              if(!interreg.test(inter.value)){
                      alert("提示\n\n请输入整数");
                      interreg.focus();
                      return false;
              }
                                   var  ffinter= document.getElementById("ffinter");
              //正整数+0校验
              var ffinterreg =/^[1-9]\d*|0$/;

              if(!ffinterreg.test(ffinter.value)){
                      alert("提示\n\n请输入非负整数");
                     ffinterreg.focus();
                      return false;
              }
                                    var  finter= document.getElementById("finter");
              //负整数+0校验
              var finterreg =/^-[1-9]\d*|0$/;

              if(!finterreg.test(finter.value)){
                      alert("提示\n\n请输入负整数");
                     finterreg.focus();
                      return false;
              }
                                    var  zboolean= document.getElementById("zboolean");
              //正浮点数
              var zbooleanreg =/^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$/;

              if(!zbooleanreg.test(zboolean.value)){
                      alert("提示\n\n请输入负整数");
                     zbooleanreg.focus();
                      return false;
              }
                                   var  fboolean= document.getElementById("fboolean");
              //负浮点数
              var fbooleanreg =/^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$/;

              if(!fbooleanreg.test(fboolean.value)){
                      alert("提示\n\n请输入负浮点数");
                     fbooleanreg.focus();
                      return false;
              }
                                     var  bboolean= document.getElementById("bboolean");
              //浮点数
              var bbooleanreg =/^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$/;

              if(!bbooleanreg.test(bboolean.value)){
                      alert("提示\n\n请输入浮点数");
                     bbooleanreg.focus();
                      return false;
              }
                                    var  ffboolean= document.getElementById("ffboolean");
              //非负浮点数
              var ffbooleanreg =/^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$/;

              if(!ffbooleanreg.test(ffboolean.value)){
                      alert("提示\n\n请输入非负浮点数");
                     ffbooleanreg.focus();
                      return false;
              }
                                     var  fzboolean= document.getElementById("fzboolean");
              //非正浮点数
              var fzbooleanreg =/^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$/;

              if(!fzbooleanreg.test(fzboolean.value)){
                      alert("提示\n\n请输入非正浮点数");
                     fzbooleanreg.focus();
                      return false;
              }
                                     var  letter= document.getElementById("letter");
              //字母验证
              var letterreg =/^[A-Za-z]+$/;

              if(!letterreg.test(letter.value)){
                      alert("提示\n\n请输入字母");
                     letterreg.focus();
                      return false;
              }
                                      var  dletter= document.getElementById("dletter");
              //大写字母验证
              var dletterreg =/^[A-Z]+$/;

              if(!dletterreg.test(dletter.value)){
                      alert("提示\n\n请输入大写英文字母");
                     dletterreg.focus();
                      return false;
              }
                                    var  xletter= document.getElementById("xletter");
              //小写字母验证
              var xletterreg =/^[a-z]+$/;

              if(!xletterreg.test(xletter.value)){
                      alert("提示\n\n请输入小写英文字母");
                     xletterreg.focus();
                      return false;
              }
                                     var  szzm= document.getElementById("szzm");
              //字母数字验证
              var szzmreg =/^[A-Za-z0-9]+$/;

              if(!szzmreg.test(szzm.value)){
                      alert("提示\n\n请输入英文字母和数字");
                     szzmreg.focus();
                      return false;
              }
                                   var  tszzm= document.getElementById("tszzm");
              //字母数字验证
              var tszzmreg =/^\w+$/;

              if(!tszzmreg.test(tszzm.value)){
                      alert("提示\n\n请输入英文字母或数字或下划线");
                     tszzmreg.focus();
                      return false;
              }
                                  }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/312566
推荐阅读
相关标签
  

闽ICP备14008679号