可以用"我佛山人"的validator.js, 非常好用,可以设置出错后的显示模式
[code="java"]
表单验证类 Validator v1.05
<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;overflow:auto;}<br>
form{display:inline}<br>
身份证号: |
|
真实姓名: |
|
ID: |
|
英文名: |
|
主页: |
|
密码: |
|
重复: |
|
信箱: |
|
信箱: |
|
QQ: |
|
身份证: |
|
年龄: |
|
年龄1: |
|
电话: |
|
手机: |
|
生日: |
|
邮政编码: |
|
邮政编码: |
|
操作系统: |
选择您所用的操作系统Win98Win2kWinXP |
所在省份: |
广东陕西浙江江西 |
爱好: |
运动上网听音乐看书 |
自我介绍: |
中文是一个字 |
自传: |
中文是两个字节t |
相片上传: |
|
|
<br>
/*************************************************<br>
Validator v1.05<br>
code by 我佛山人<br>
<a href="mailto:wfsr@msn.com">wfsr@msn.com</a><br>
*************************************************/<br>
Validator = {<br>
Require : /.+/,<br>
Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*.\w+([-.]\w+)*$/,<br>
Phone : /^(((\d{2,3}))|(\d{3}-))?((0\d{2,3})|0\d{2,3}-)?[1-9]\d{6,7}(-\d{1,4})?$/,<br>
Mobile : /^(((\d{2,3}))|(\d{3}-))?13\d{9}$/,<br>
Url : /^http:\/\/[A-Za-z0-9]+.[A-Za-z0-9]+[\/=\?%-&_~`@[]\':+!]*([^<>\"\"])*$/,<br>
IdCard : "this.IsIdCard(value)",<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>
Username : /^[a-z]\w{3,}$/i,<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>
Filter : "this.DoFilter(value, getAttribute('accept'))",<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|0) && (value|0) < 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\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 "IdCard" :<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>
case "Filter" :<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>
},<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>
DoFilter : function(input, filter){<br>
return new RegExp("^.+.(?=EXT)(EXT)$".replace(/EXT/g, filter.split(/\s*,\s*/).join("|")), "gi").test(input);<br>
},<br>
IsIdCard : function(number){<br>
var date, Ai;<br>
var verify = "10x98765432";<br>
var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];<br>
var area = ['','','','','','','','','','','','北京','天津','河北','山西','内蒙古','','','','','','辽宁','吉林','黑龙江','','','','','','','','上海','江苏','浙江','安微','福建','江西','山东','','','','河南','湖北','湖南','广东','广西','海南','','','','重庆','四川','贵州','云南','西藏','','','','','','','陕西','甘肃','青海','宁夏','新疆','','','','','','台湾','','','','','','','','','','香港','澳门','','','','','','','','','国外'];<br>
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);<br>
if(re == null) return false;<br>
if(re[1] >= area.length || area[re[1]] == "") return false;<br>
if(re[2].length == 12){<br>
Ai = number.substr(0, 17);<br>
date = [re[9], re[10], re[11]].join("-");<br>
}<br>
else{<br>
Ai = number.substr(0, 6) + "19" + number.substr(6);<br>
date = ["19" + re[4], re[5], re[6]].join("-");<br>
}<br>
if(!this.IsDate(date, "ymd")) return false;<br>
var sum = 0;<br>
for(var i = 0;i<=16;i++){<br>
sum += Ai.charAt(i) * Wi[i];<br>
}<br>
Ai += verify.charAt(sum%11);<br>
return (number.length ==15 || number.length == 18 && number == Ai);<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]*1;<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]*1;<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==0 ?12:month;<br>
var date = new Date(year, month-1, day);<br>
return (typeof(date) == "object" && year == date.getFullYear() && month == (date.getMonth()+1) && day == date.getDate());<br>
function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;}<br>
}<br>
}<br>
[/code]