超级强大的表单验证_JavaScript
Libérer: 2016-05-16 19:28:59
original
999 Les gens l'ont consulté
表单验证类 Validator v1.01
<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>
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31