Home > Web Front-end > JS Tutorial > body text

Super powerful form validation_JavaScript

WBOY
Release: 2016-05-16 19:28:59
Original
953 people have browsed it
Form validation class Validator v1.01
body,td{font:normal 12px Verdana;color:#333333}
input,textarea,select,td {font:normal 12px Verdana;color:#333333;border:1px solid #999999;background:#ffffff} table{border-collapse:collapse;}
td{padding:3px} input{ height:20;}
textarea{width:80%;height:50px;overfmin:auto;} form{display:inline}


Real name:


English name:


Password:


Duplicate:


Mailbox:


Mailbox:


QQ:

🎜> Age:


Age 1:


Tel:


Mobile phone:



Postcode:

 
 
  
 
 
  
 
 
  
 
 
  
 
  
 
    
 
 
  
 
 
 
>
邮政编码:
操作系统:
所在省份: 广东陕西浙江江西
爱好: 运动上网听音乐看书
自我介绍: 自传:

 <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>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!