String.prototype.format = function(){ var tmpStr = this; var iLen = arguments.length; for(var i=0;i 30?1900:2000); d = new Date(o.yyyy, o.MM - 1, o.dd, o.hh, o.mm, o.ss, o.ms); var reVal = d.getFullYear() == o.yyyy && d.getMonth() + 1 == o.MM && d.getDate() == o.dd && d.getHours() == o.hh && d.getMinutes() == o.mm && d.getSeconds() == o.ss && d.getMilliseconds() == o.ms; if(reVal && arguments.length == 2) return d; else return reVal; function s(s1, s2, s3, s4, s5){ var y1 = typeof(s1) == "undefined"; var y2 = typeof(s2) == "undefined"; s4 = s4 || 60; s5 = s5 || 2; var reVal = s1; if(y1 && y2) reVal = s3; else if(!y1){ if(s1.length != s5) return false; else if(s1 == "" || isNaN(s1)) reVal = s3; } else{ if(s2 == "" || isNaN(s2)) reVal = s3; else reVal = s2; } reVal *= 1; if(reVal > s4)return false; return reVal; } } function $() { var elements = new Array(); for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (typeof element == 'string'){ element = document.getElementById(element) || document.getElementsByName(element)[0]; } if (arguments.length == 1) return element; elements.push(element); } return elements; } var Validator = { Version : '3.0.0', Author : '我佛山人', toString : function(){ return ["关于Validator 3.0.0", "Version:" + this.Version, "Author:" + this.Author].join("\t\t\t\n\n"); }, Xml : null, XmlReader : function(){ this.GetHttpRequest = function(){ if (window.XMLHttpRequest) return new XMLHttpRequest(); else if (window.ActiveXObject) return new ActiveXObject("MsXml2.XmlHttp") ; } this.LoadUrl = function(urlToCall, asyncFunctionPointer, refObjectName){ var oXmlReader = this; var bAsync = (typeof(asyncFunctionPointer) == "function"); var oXmlHttp = this.GetHttpRequest(); oXmlHttp.open("GET", urlToCall, bAsync); if(Validator.Config.Debug){ oXmlHttp.setRequestHeader("pragma","no-cache"); oXmlHttp.setRequestHeader("cache-control","no-cache"); oXmlHttp.setRequestHeader("expires","0"); //oXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); } if (bAsync){ oXmlHttp.onreadystatechange = function(){ if (oXmlHttp.readyState == 4){ oXmlReader.DOMDocument = oXmlHttp.responseXML; if (oXmlHttp.status == 200) { if(typeof(refObjectName) == "string"){ Validator.Config.Settings[refObjectName].DataSource = oXmlReader; asyncFunctionPointer(oXmlReader, refObjectName); } else asyncFunctionPointer(oXmlReader) } else alert("XML request error: " + oXmlHttp.statusText + " (" + oXmlHttp.status + ")" ) ; } } } oXmlHttp.send(null) ; if (!bAsync){ if (oXmlHttp.status == 200) this.DOMDocument = oXmlHttp.responseXML ; else {alert("XML request error: " + oXmlHttp.statusText + " (" + oXmlHttp.status + ")") ;} } } this.SelectNodes = function(xpath){ if (document.all) return this.DOMDocument.selectNodes(xpath) ; else{ var aNodeArray = new Array(); var xPathResult = this.DOMDocument.evaluate(xpath, this.DOMDocument, this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ; if (xPathResult){ var oNode = xPathResult.iterateNext() ; while(oNode){ aNodeArray[aNodeArray.length] = oNode ; oNode = xPathResult.iterateNext(); } } return aNodeArray ; } } this.SelectSingleNode = function(xpath){ if (document.all)return this.DOMDocument.selectSingleNode(xpath) ; else{ var xPathResult = this.DOMDocument.evaluate(xpath, this.DOMDocument, this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), 9, null); if (xPathResult && xPathResult.singleNodeValue) return xPathResult.singleNodeValue ; else return null ; } } }, Config : { Debug : false, Default : { Name : null, DataSourceType : 2, DataSource : null, XmlPath : '/plus/validator/validation.xml', XPath : {Path : location.pathname.toLowerCase(), Name : null}, ValidateType : 1, AlertType : 8, Summary : null, WarnColor : 'red', SuccessColor : 'green', Errors : { Title : '发生以下错误,提交失败,请逐条检查输入:', Items : [], Add : function(e){ this.Items[this.Items.length] = e; }, Clear : function(){this.Items.length = 0} } }, Settings : {}, Forms : {}, Add : function(formID, elements){ if(Validator.Config.Forms[formID]){ var e; for(e in elements)Validator.Config.Forms[formID][e] = elements[e]; } else { Validator.Config.Forms[formID] = elements; Validator.Config.Forms[formID].id = formID; } } }, Enumerate : { ValidateType : { Blur : 1, Submit : 2 }, DataSourceType : { Xml : 1, Attribute : 2, Object : 4 }, AlertType : { AlertSingle : 1, AlertAll : 2, Color : 4, SummarySingle : 8, SummaryAll : 16 } }, DataType : { Regex : { Require : /.+/, 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{2,3}\))|(\d{3}\-))?13\d{9}$/, Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/, IP : /^(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5])$/, Currency : /^\d+(\.\d+)?$/, Number : /^\d+$/, Zip : /^[1-9]\d{5}$/, QQ : /^[1-9]\d{4,8}$/, Integer : /^[-\+]?\d+$/, Double : /^[-\+]?\d+(\.\d+)?$/, English : /^[A-Za-z]+$/, Chinese : /^[\u0391-\uFFE5]+$/, Username : /^[A-Za-z\d\u0391-\uFFE5][\w\u0391-\uFFE5]{1,19}$/i, UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/ }, Element : { IElement : function(){ this.Name = null; this.DataType = null; this.Message = null; this.Tips = null; this.DependOn = null; this.Require = true; this.IsValid = false; this.GetValue = function(){ if($(this.Name)) return $(this.Name).value; } this.toString = function(){return '[Validator Element]';} this.Validate = function(){ if(this.DependOn != null){try{eval(this.DependOn);}catch(e){if(Validator.Cofig.Debug)alert(e.description);}}; this.IsValid = (!this.Require || this.Require == "false" || this.Require == "0") && this.GetValue() == "" || this.Process(); return this.IsValid; } this.Throw = function(){ var oForm = arguments[0]; Validator.Config.Settings[oForm.id].Errors.Add(this); if((this.AlertType & Validator.Enumerate.AlertType.SummarySingle) == Validator.Enumerate.AlertType.SummarySingle){ var container = $(this.Name + "__MessagePanel"); if(!container){ container = document.createElement("span"); with(container){ id = this.Name + "__MessagePanel"; className = "Validator_MessagePanel_Warn"; } $(this.Name).parentNode.appendChild(container); } container.innerHTML = this.Message; container.style.display = ""; } if((this.AlertType & Validator.Enumerate.AlertType.Color) == Validator.Enumerate.AlertType.Color){ $(this.Name).style.color = Validator.Config.Settings[oForm.id].WarinColor; } if((this.AlertType & Validator.Enumerate.AlertType.AlertSingle) == Validator.Enumerate.AlertType.AlertSingle && Validator.Config.Settings[oForm.id].Errors.Items.length == 1){ alert(this.Message); this.Focus(); } } this.ShowTips = function(){ var container = $(this.Name + "__MessagePanel"); if(!container){ container = document.createElement("span"); with(container){ id = this.Name + "__MessagePanel"; className = "Validator_MessagePanel_Success"; } $(this.Name).parentNode.appendChild(container); } container.innerHTML = this.Tips; container.style.display = ""; } this.HideMessage = function(){ if($(this.Name + "__MessagePanel"))$(this.Name + "__MessagePanel").style.display = "none"; } this.Focus = function(){try{$(this.Name).focus();}catch(e){if(Validator.Config.Debug)alert(e.description)}} this.OnBlur = function(){ this.Validate(); if(!this.IsValid) this.Throw(Validator.Config.Forms[$(this.Name).form.id]); else this.HideMessage(); } this.Process = function(){alert('未实现IElement.Process接口');return false;} }, RegexElement : function(){ this.base = Validator.DataType.Element.IElement; this.base(); this.Pattern = null; this.GetValue = function(){ if($(this.Name)) return $(this.Name).value; } this.Process = function(){ this.Pattern = Validator.DataType.Regex[this.DataType]; return this.Pattern != null && this.Pattern.test(this.GetValue()); } }, DateTimeElement : function(){ this.base = Validator.DataType.Element.IElement; this.base(); this.Format = "yyyy-MM-dd"; this.Process = function(){ return this.GetValue().isDateTime(this.Format); } }, PasswordElement : function(){ this.base = Validator.DataType.Element.IElement; this.base(); this.Regulars = { low : /^\w{1,3}$/, medium : /^(\d{1,5}|[a-z]{1,5}|[A-Z]{1,5})$/, high : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/ } this.Level = "high"; this.Process = function(){ return !this.Regulars[this.Level.toLowerCase()].test(this.GetValue()); } }, RangeElement : function(){ this.base = Validator.DataType.Element.IElement; this.base(); this.Min = 0; this.MinElement = null; this.Max = 100; this.MaxElement = null; this.Value = 0; this.Format = "yyyy-MM-dd"; this.ParseType = "Number"; this.Types = { String : function(){this.Format = null}, CaseInsensitiveString : function(){this.Value = this.Value.toUpperCase();this.Min = this.Mim.toUpperCase();this.Max = this.Max.toUpperCase();}, Number : function(){this.Value = this.Value*1;this.Min = this.Min*1;this.Max = this.Max * 1;}, DateTime : function(){this.Value = this.Value.isDateTime(this.Format, true);this.Min = this.Min.isDateTime(this.Format, true);this.Max = this.Max.isDateTime(this.Format, true);}, IPAddress : function(){this.Value = Validator.Util.IPAddressFix(this.Value);this.Min = Validator.Util.IPAddressFix(this.Min);this.Max = Validator.Util.IPAddressFix(this.Max);} } this.Process = function(){ this.SetValue(); if(this.MinElement != null) this.Min = $(this.MinElement).value; if(this.MaxElement != null) this.Max = $(this.MaxElement).value; this.Types[this.ParseType].call(this, null); if(this.Value == false || this.Min == false || this.Max == false)return false; else return this.Min <= this.Value && this.Value <= this.Max; } this.SetValue = function(){ this.Value = this.GetValue(); } }, GroupElement : function(){ this.base = Validator.DataType.Element.RangeElement; this.base(); this.ParseType = "Number"; this.SetValue = function(){ this.Value = 0; var elements = document.getElementsByName(this.Name); for(var i=elements.length-1;i>-1;i--){ if(elements[i].checked) this.Value ++; } } }, LimitElement : function(){ this.base = Validator.DataType.Element.RangeElement; this.base(); this.ParseType = "Number"; this.SetValue = function(){ this.Value = this.GetValue().length; } }, LimitBElement : function(){ this.base = Validator.DataType.Element.RangeElement; this.base(); this.ParseType = "Number"; this.SetValue = function(){ this.Value = this.GetValue().replace(/[^\x00-\xff]/g,"**").length; } }, RepeatElement : function(){ this.base = Validator.DataType.Element.IElement; this.base(); this.To = null; this.Process = function(){ return this.GetValue() == $(this.To).value; } }, FilterElement : function(){ this.base = Validator.DataType.Element.IElement; this.base(); this.Accept = null; this.ParseType = "File"; this.Types = { File : function(){ return this.Accept != null && new RegExp("^.+\\.(?=EXT)(EXT)$".replace(/EXT/g, this.Accept.split(/\s*,\s*/).join("|")), "gi").test(this.GetValue()); }, Badword : function(){ return !this.Types.Keyword.call(this, null); }, Keyword : function(){ return this.Accept != null && new RegExp(this.Accept.split(/\s*,\s*/).join("|"), "gi").test(this.GetValue()); }, BeginWith : function(){ return this.Accept != null && new RegExp("^(?=EXT)(EXT)".replace(/EXT/g, this.Accept.split(/\s*,\s*/).join("|")), "gi").test(this.GetValue()); }, EndWith : function(){ return this.Accept != null && new RegExp(".*(?=EXT)(EXT)$".replace(/EXT/g, this.Accept.split(/\s*,\s*/).join("|")), "gi").test(this.GetValue()); } } this.Process = function(){ return this.Types[this.ParseType].call(this, null); } }, CustomElement : function(){ this.base = Validator.DataType.Element.IElement; this.base(); this.ParseType = "Regex"; this.Value = ""; this.Types = { Regex : function(){ var reVal = false; try{ reVal = new RegExp(this.Regex, this.Options).test(this.Value); } catch(e) { reVal = false;if(Validator.Config.Debug)alert(['正则表达式错误:', e.description].join("\n"));} return reVal; }, Script : function(){ var reVal = false; try{ eval(this.Script); } catch(e){ reVal = false;if(Validator.Config.Debug)alert(['自定义脚本错误:', e.description].join("\n"));} return reVal; }, Elements : function(){ var elements = this.Elements.split(/[^a-z\d]+/ig); var result = this.Elements; for(var i=elements.length-1;i>-1;i--){ this.DataType = elements[i]; var o, e; o = {}; for(e in this){if(typeof(this[e]) == "string")o[e] = this[e];} result = result.replace(new RegExp(elements[i], "g"), Validator.ElementFactory.Build(o).Validate()); } return eval(result); } } this.Process = function(){ this.SetValue(); return this.Types[this.ParseType].call(this, null); } this.SetValue = function(){ this.Value = this.GetValue(); } }, CompareElement : function(){ this.base = Validator.DataType.Element.IElement; this.base(); this.To = null; this.ToElement = null; this.Value = ""; this.Format = "yyyy-MM-dd"; this.ParseType = "Number"; this.Types = { String : function(){this.Format = null}, CaseInsensitiveString : function(){this.Value = this.Value.toUpperCase();this.To = this.To.toUpperCase();}, Number : function(){this.Value = this.Value*1;this.To = this.To*1}, DateTime : function(){this.Value = this.Value.isDateTime(this.Format, true);this.To = this.To.isDateTime(this.Format, true);}, IPAddress : function(){this.Value = Validator.Util.IPAddressFix(this.Value);this.To = Validator.Util.IPAddressFix(this.To);} } this.Operator = "Equal"; this.Operators = { NotEqual : 'return this.Value != this.To', GreaterThan : 'this.Value > this.To', GreaterThanEqual : 'this.Value >= this.To', LessThan : 'this.Value < this.To', LessThanEqual : 'this.Value <= this.To', Equal : 'this.Value == this.To' } this.Process = function(){ if(this.ToElement != null) this.To = $(this.ToElement).value; this.Value = this.GetValue(); this.Types[this.ParseType].call(this, null); return eval(this.Operators[this.Operator]) } }, IdCardElement : function(){ this.base = Validator.DataType.Element.IElement; this.base(); this.To = null; this.Process = function(){ var number = this.GetValue().toLowerCase(); var date, Ai; var verify = "10x98765432"; var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; var area = ['','','','','','','','','','','','北京','天津','河北','山西','内蒙古','','','','','','辽宁','吉林','黑龙江','','','','','','','','上海','江苏','浙江','安微','福建','江西','山东','','','','河南','湖北','湖南','广东','广西','海南','','','','重庆','四川','贵州','云南','西藏','','','','','','','陕西','甘肃','青海','宁夏','新疆','','','','','','台湾','','','','','','','','','','香港','澳门','','','','','','','','','国外']; var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/i); if(re == null) return false; if(re[1] >= area.length || area[re[1]] == "") return false; if(re[2].length == 12){ Ai = number.substr(0, 17); date = [re[9], re[10], re[11]].join("-"); } else{ Ai = number.substr(0, 6) + "19" + number.substr(6); date = ["19" + re[4], re[5], re[6]].join("-"); } if(!date.isDateTime("yyyy-MM-dd")) return false; var sum = 0; for(var i = 0;i<=16;i++){ sum += Ai.charAt(i) * Wi[i]; } Ai += verify.charAt(sum%11); return (number.length ==15 || number.length == 18 && number == Ai); } } }, Extend : {} }, ElementFactory : { Build : function(o){ var element; var dataType = o.DataType; if(dataType == "Date" || dataType == "Time") dataType = "DataTime"; dataType += "Element"; if(typeof(Validator.DataType.Element[dataType]) == "function"){ element = new Validator.DataType.Element[dataType](); } else if(typeof(Validator.DataType.Extend[dataType]) == "function"){ element = new Validator.DataType.Extend[dataType](); } else {element = new Validator.DataType.Element.RegexElement();} var eo; for(eo in o) element[eo] = o[eo]; return element; } }, Setup : function(){ var e; if(arguments.length == 0 || typeof(arguments[0]) != "object") return ; var o = arguments[0]; for(var i=arguments.length-1;i>-1;i--){ Validator.Config.Settings[arguments[i].Name] = Validator.Config.Default; for(e in arguments[i]) Validator.Config.Settings[arguments[i].Name][e] = arguments[i][e]; with(Validator.Config.Settings[arguments[i].Name]){ if((DataSourceType & Validator.Enumerate.DataSourceType.Xml) == Validator.Enumerate.DataSourceType.Xml){ if(Validator.Xml == null)new Validator.XmlReader().LoadUrl(XmlPath, Validator.XmlReaderCallBack, arguments[i].Name); else Validator.XmlReaderCallBack(Validator.Xml, arguments[i].Name); } } } }, Validate : function(form){ var oForm = Validator.Config.Forms[form.id]; var e, ie; var reVal = true; Validator.Config.Settings[form.id].Errors.Clear(); var alertType = Validator.Config.Settings[form.id].AlertType; for(e in oForm){ if(e == "id")continue; if(!oForm[e].Validate()) {reVal = false;oForm[e].Throw(oForm);} else oForm[e].HideMessage(); } with(Validator.Config.Settings[form.id].Errors){ var len = Items.length; var alertType = Validator.Config.Settings[form.id].AlertType; if((alertType & Validator.Enumerate.AlertType.AlertAll) == Validator.Enumerate.AlertType.AlertAll && !reVal){ var errors = []; for(var i=0;i"; } if($(Validator.Config.Settings[form.id].Summary)){ $(Validator.Config.Settings[form.id].Summary).innerHTML = "
  • " + Title + "
  • \n" + errors.join("\n"); $(Validator.Config.Settings[form.id].Summary).style.display = ""; } } } return reVal; }, KeyboardHandler : { NumberFilter : function(e){ if(e.keyCode <49 || e.keyCode >59) return false; } }, MouseHandler : { }, EventHandler : { OnBlur : function(e){ e = event.srcElement || e; var name = e.id || e.name; Validator.Config.Forms[e.form.id][name].OnBlur(); } }, XmlReaderCallBack : function(xml, formID){ if(Validator.Xml == null) Validator.Xml = xml; var elements = xml.SelectNodes("//form[@Path='" + location.pathname.toLowerCase() + "' and @Name='" + formID + "']/item");//xml.DOMDocument.getElementsByTagName("item"); var len = elements.length; var alertType = Validator.Config.Settings[formID].AlertType; if(len == 0)return; var elementObjects = {}; for(var i=0;i 0) obj.Script = elements[i].childNodes[0].nodeValue; if(!obj.AlertType) obj.AlertType = alertType; elementObjects[obj.Name] = Validator.ElementFactory.Build(obj); } Validator.Config.Add(formID, elementObjects) }, AttributeReader : function(formID){ var elements = document.forms[formID].elements; var len = elements.length; var alertType = Validator.Config.Settings[formID].AlertType; if(len == 0)return; var elementObjects = {}; var customAttributes = "DataType,Message,Require,To,Min,Max,ToElement,ParseType,MinElement,MaxElement,Format,Elements,Accept,DependOn,Level,Script,Pattern,Operator,AlertType,name,id".split(","); var len1 = customAttributes.length; for(var i=0;i