<font face="Verdana"> <title>表单验证类 Validator v1.01</title> <br> <style><BR> body,td{font:normal 12px Verdana;color:#333333}<BR> input,textarea,select,td{font:normal 12px Verdana;color:#333333;border:1px solid #999999;background:#ffffff}<BR> table{border-collapse:collapse;}<BR> td{padding:3px}<BR> input{height:20;}<BR> textarea{width:80%;height:50px;overfmin:auto;}<BR> form{display:inline}<BR> </style> <br> <table align="center"> <br> <form name="theForm" id="demo" action="" method="get" onsubmit="return Validator.Validate(this,2)"> <br> <tr> <br> <td>真实姓名:</td> <td><input name="Name" datatype="Chinese" msg="真实姓名只允许中文"></td> <br> </tr> <br> <tr> <br> <td>英文名:</td> <td><input name="Nick" datatype="English" require="false" msg="英文名只允许英文字母"></td> <br> </tr> <br> <tr> <br> <td>主页:</td> <td><input name="Homepage" require="false" datatype="Url" msg="非法的Url"></td> <br> </tr> <br> <tr> <br> <td>密码:</td> <td><input name="Password" datatype="SafeString" msg="密码不符合安全规则" type="password"></td> <br> </tr> <br> <tr> <br> <td>重复:</td> <td><input name="Repeat" datatype="Repeat" to="Password" msg="两次输入的密码不一致" type="password"></td> <br> </tr> <br> <tr> <br> <td>信箱:</td> <td><input name="Email" datatype="Email" msg="信箱格式不正确"></td> <br> </tr> <br> <tr> <br> <td>信箱:</td> <td><input name="Email" datatype="Repeat" to="Email" msg="两次输入的信箱不一致"></td> <br> </tr> <br> <tr> <br> <td>QQ:</td> <td><input name="QQ" require="false" datatype="QQ" msg="QQ号码不存在"></td> <br> </tr> <br> <tr> <br> <td>身份证:</td> <td><input name="Card" datatype="IdCard" msg="身份证号码不正确"></td> <br> </tr> <br> <tr> <br> <td>年龄:</td> <td><input name="Year" datatype="Range" msg="年龄必须在18~28之间" min="18" max="28"></td> <br> </tr> <br> <tr> <br> <td>年龄1:</td> <td><input name="Year1" require="false" datatype="Compare" msg="年龄必须在18以上" to="18" operator="GreaterThanEqual"></td> <br> </tr> <br> <tr> <br> <td>电话:</td> <td><input name="Phone" require="false" datatype="Phone" msg="电话号码不正确"></td> <br> </tr> <br> <tr> <br> <td>手机:</td> <td><input name="Mobile" require="false" datatype="Mobile" msg="手机号码不正确"></td> <br> </tr> <br> <tr> <br> <td>生日:</td> <td><input name="Birthday" datatype="Date" format="ymd" msg="生日日期不存在"></td> <br> </tr> <br> <tr> <br> <td>邮政编码:</td> <td><input name="Zip" datatype="Custom" regexp="^[1-9]\d{5}$" msg="邮政编码不存在"></td> <br> </tr> <br> <tr> <br> <td>邮政编码:</td> <td><input name="Zip1" datatype="Zip" msg="邮政编码不存在"></td> <br> </tr> <br> <tr> <br> <td>操作系统:</td> <td><select name="Operation" datatype="Require" msg="未选择所用操作系统"><option value="">选择您所用的操作系统</option> <option value="Win98">Win98</option> <option value="Win2k">Win2k</option> <option value="WinXP">WinXP</option></select></td> <br> </tr> <br> <tr> <br> <td>所在省份:</td> <td>广东<input name="Province" value="1" type="radio">陕西<input name="Province" value="2" type="radio">浙江<input name="Province" value="3" type="radio">江西<input name="Province" value="4" type="radio" datatype="Group" msg="必须选定一个省份"> </td> <br> </tr> <br> <tr> <br> <td>爱好:</td> <td>运动<input name="Favorite" value="1" type="checkbox">上网<input name="Favorite" value="2" type="checkbox">听音乐<input name="Favorite" value="3" type="checkbox">看书<input name="Favorite" value="4" type="checkbox" datatype="Group" min="2" max="3" msg="必须选择2~3种爱好"> </td> <br> </tr> <br> <td>自我介绍:</td> <td><textarea name="Description" datatype="Limit" max="10" msg="自我介绍内容必须在10个字之内">中文是一个字</textarea></td> <br> <br> <td>自传:</td> <td><textarea name="History" datatype="LimitB" min="3" max="10" msg="自传内容必须在[3,10]个字节之内">中文是两个字节t</textarea></td> <br> <br> <tr> <br> <td colspan="2"> <input name="Submit" type="submit" value="确定提交"><input onclick="Validator.Validate(document.getElementById('demo'))" value="检验模式1" type="button"><input onclick="Validator.Validate(document.getElementById('demo'),2)" value="检验模式2" type="button"><input onclick="Validator.Validate(document.getElementById('demo'),3)" value="检验模式3" type="button"> </td> <br> </tr> <br> </form> <br> </table> <br> <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></font>