js正则表达式 未提交进行验证

转载请注明来源:https://www.cnblogs.com/hookjc/

function SetInputMust()

 /*var varr=idstr.split(',');
 idstr=idstr.replace(/,/g,',,');  
 idstr=','+idstr+','; */
 var smsg='';
 var Validity={
 Require : /.+/,
 IP : /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,
 Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
 Phone : /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/,
 Mobile : /^((\(\d{3}\))|(\d{3}\-))?13\d{9}|1[0-9]\d{9}$/,
 Url : /(^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$)|(^[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$)/, 
 Currency : /^\d+(\.\d+)?$/,
 Number : /^\d+(\.\d+)?$/,
 Zip : /^[1-9]\d{5}$/,
 QQ : /^[1-9]\d{4,12}$/,
 Integer : /^[-\+]?\d+$/,
 Double : /^[-\+]?\d+(\.\d+)?$/,
 English : /^[A-Za-z]+$/,
 Chinese :  /^[\Α-\¥]+$/,
 Username : /^[a-z]\w{3,}$/i,
 Idcard : /^d{15}|d{}18$/
 };

 
for(i=0;i<$("input").size();++i)
 {
  var msg="您输入的信息有误!";
  var inputobj=$("input")[i];
  if(typeof($(inputobj).attr("dataType"))!='undefined')
  {
   if(typeof($(inputobj).attr("msg"))!='undefined')
    msg=$(inputobj).attr("msg");
   var stest="Validity."+$(inputobj).attr("dataType")+".test('"+val+"')";
   stest="if(!"+stest+") smsg+='"+msg+"\\n';";    
   try{eval(stest);}catch(e){alert(stest);}
  }   
 } 

/*
 for(i=0;i<varr.length;++i)
 {  
  var gid="#"+varr[i];  
  var iid=","+$(gid).attr("id")+",";
  var val=$.trim($(gid).val());
  if(idstr.indexOf(iid)!=-1)
  {
   var msg='您输入的信息有误!';    
   if(typeof($(gid).attr("msg"))!='undefined')
    msg=$(gid).attr("msg");     
   if(typeof($(gid).attr("dataType"))!='undefined')
   {
    var stest="Validity."+$(gid).attr("dataType")+".test('"+val+"')";
    stest="if(!"+stest+") smsg+='"+msg+"\\n';";    
    try{eval(stest);}catch(e){alert(stest);}
   }
   else if(val=='')
    smsg+=msg+"\n";
  }
 }*/
 if(smsg!='') {alert(smsg); return false;}
 return true;
};

 

使用:

<input type="text" name="num" id="num" size="4" dataType="Integer" msg="输入格式有误!">

<input type="button" name="btn" name="btn" onClick="SetInputMust();">

来源:python脚本自动迁移

posted @ 2020-06-29 09:34  jiangcheng_15  阅读(168)  评论(0编辑  收藏  举报