Heim > Web-Frontend > js-Tutorial > 超级强大的表单验证_JavaScript

超级强大的表单验证_JavaScript

WBOY
Freigeben: 2016-05-16 19:28:59
Original
998 Leute haben es durchsucht
 表单验证类 Validator v1.01
 
 
 
   
  
 
 
  
 
   
  
 
 
  
 
 
  
 
 
  
 
   
  
 
 
  
 
   
  
 
 
  
 
  
  
 
  
  
 
  
  
 
    
  
 
  
  
 
 
  
 
 
  
 
 
  
 
 
  
 
  
 
    
 
 
  
 
 
 
真实姓名:
英文名:
主页:
密码:
重复:
信箱:
信箱:
QQ:
身份证:
年龄:
年龄1:
电话:
手机:
生日:
邮政编码:
邮政编码:
操作系统:
所在省份: 广东陕西浙江江西
爱好: 运动上网听音乐看书
自我介绍: 自传:

 <script><BR> /*************************************************<BR> Validator v1.01<BR> code by 我佛山人<BR> wfsr@cunite.com<BR> http://www.cunite.com<BR>*************************************************/<BR> Validator = {<BR> Require : /.+/,<BR> Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,<BR> Phone : /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/,<BR> Mobile : /^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/,<BR> Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,<BR> IdCard : /^\d{15}(\d{2}[A-Za-z0-9])?$/,<BR> Currency : /^\d+(\.\d+)?$/,<BR> Number : /^\d+$/,<BR> Zip : /^[1-9]\d{5}$/,<BR> QQ : /^[1-9]\d{4,8}$/,<BR> Integer : /^[-\+]?\d+$/,<BR> Double : /^[-\+]?\d+(\.\d+)?$/,<BR> English : /^[A-Za-z]+$/,<BR> Chinese : /^[\u0391-\uFFE5]+$/,<BR> UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,<BR> IsSafe : function(str){return !this.UnSafe.test(str);},<BR> SafeString : "this.IsSafe(value)",<BR> Limit : "this.limit(value.length,getAttribute('min'), getAttribute('max'))",<BR> LimitB : "this.limit(this.LenB(value), getAttribute('min'), getAttribute('max'))",<BR> Date : "this.IsDate(value, getAttribute('min'), getAttribute('format'))",<BR> Repeat : "value == document.getElementsByName(getAttribute('to'))[0].value",<BR> Range : "getAttribute('min') < value && value < getAttribute('max')",<BR> Compare : "this.compare(value,getAttribute('operator'),getAttribute('to'))",<BR> Custom : "this.Exec(value, getAttribute('regexp'))",<BR> Group : "this.MustChecked(getAttribute('name'), getAttribute('min'), getAttribute('max'))",<BR> ErrorItem : [document.forms[0]],<BR> ErrorMessage : ["以下原因导致提交失败: \t\t\t"],<BR> Validate : function(theForm, mode){<BR> var obj = theForm || event.srcElement;<BR> var count = obj.elements.length;<BR> this.ErrorMessage.length = 1;<BR> this.ErrorItem.length = 1;<BR> this.ErrorItem[0] = obj;<BR> for(var i=0;i<count;i++){<BR> with(obj.elements[i]){<BR> var _dataType = getAttribute("dataType");<BR> if(typeof(_dataType) == "object" || typeof(this[_dataType]) == "undefined") continue;<BR> this.ClearState(obj.elements[i]);<BR> if(getAttribute("require") == "false" && value == "") continue;<BR> switch(_dataType){<BR> case "Date" :<BR> case "Repeat" :<BR> case "Range" :<BR> case "Compare" :<BR> case "Custom" :<BR> case "Group" : <BR> case "Limit" :<BR> case "LimitB" :<BR> case "SafeString" :<BR> if(!eval(this[_dataType])) {<BR> this.AddError(i, getAttribute("msg"));<BR> }<BR> break;<BR> default :<BR> if(!this[_dataType].test(value)){<BR> this.AddError(i, getAttribute("msg"));<BR> }<BR> break;<BR> }<BR> }<BR> }<BR> if(this.ErrorMessage.length > 1){<BR> mode = mode || 1;<BR> var errCount = this.ErrorItem.length;<BR> switch(mode){<BR> case 2 :<BR> for(var i=1;i<errCount;i++)<BR> this.ErrorItem[i].style.color = "red";<BR> case 1 :<BR> alert(this.ErrorMessage.join("\n"));<BR> this.ErrorItem[1].focus();<BR> break;<BR> case 3 :<BR> for(var i=1;i<errCount;i++){<BR> try{<BR> var span = document.createElement("SPAN");<BR> span.id = "__ErrorMessagePanel";<BR> span.style.color = "red";<BR> this.ErrorItem[i].parentNode.appendChild(span);<BR> span.innerHTML = this.ErrorMessage[i].replace(/\d+:/,"*");<BR> }<BR> catch(e){alert(e.description);}<BR> }<BR> this.ErrorItem[1].focus();<BR> break;<BR> default :<BR> alert(this.ErrorMessage.join("\n"));<BR> break;<BR> }<BR> return false;<BR> }<BR> return true;<BR> },<BR> limit : function(len,min, max){<BR> min = min || 0;<BR> max = max || Number.MAX_VALUE;<BR> return min <= len && len <= max;<BR> },<BR> LenB : function(str){<BR> return str.replace(/[^\x00-\xff]/g,"**").length;<BR> },<BR> ClearState : function(elem){<BR> with(elem){<BR> if(style.color == "red")<BR> style.color = "";<BR> var lastNode = parentNode.childNodes[parentNode.childNodes.length-1];<BR> if(lastNode.id == "__ErrorMessagePanel")<BR> parentNode.removeChild(lastNode);<BR> }<BR> },<BR> AddError : function(index, str){<BR> this.ErrorItem[this.ErrorItem.length] = this.ErrorItem[0].elements[index];<BR> this.ErrorMessage[this.ErrorMessage.length] = this.ErrorMessage.length + ":" + str;<BR> },<BR> Exec : function(op, reg){<BR> return new RegExp(reg,"g").test(op);<BR> },<BR> compare : function(op1,operator,op2){<BR> switch (operator) {<BR> case "NotEqual":<BR> return (op1 != op2);<BR> case "GreaterThan":<BR> return (op1 > op2);<BR> case "GreaterThanEqual":<BR> return (op1 >= op2);<BR> case "LessThan":<BR> return (op1 < op2);<BR> case "LessThanEqual":<BR> return (op1 <= op2);<BR> default:<BR> return (op1 == op2); <BR> }<BR> },<BR> MustChecked : function(name, min, max){<BR> var groups = document.getElementsByName(name);<BR> var hasChecked = 0;<BR> min = min || 1;<BR> max = max || groups.length;<BR> for(var i=groups.length-1;i>=0;i--)<BR> if(groups[i].checked) hasChecked++;<BR> return min <= hasChecked && hasChecked <= max;<BR> },<BR> IsDate : function(op, formatString){<BR> formatString = formatString || "ymd";<BR> var m, year, month, day;<BR> switch(formatString){<BR> case "ymd" :<BR> m = op.match(new RegExp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$"));<BR> if(m == null ) return false;<BR> day = m[6];<BR> month = m[5]--;<BR> year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));<BR> break;<BR> case "dmy" :<BR> m = op.match(new RegExp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$"));<BR> if(m == null ) return false;<BR> day = m[1];<BR> month = m[3]--;<BR> year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));<BR> break;<BR> default :<BR> break;<BR> }<BR> if(!parseInt(month)) return false;<BR> month = month==12 ?0:month;<BR> var date = new Date(year, month, day);<BR> return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate());<BR> function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;}<BR> }<BR> }<BR></script>
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage