日期:2014-05-16  浏览次数:20320 次

js验证框架

一个同事写的,分享给大家:

////注:用此验证框架,如要确保页面输入框的边框架样式好看。最好写个通用输入框样式如:input[type="text"] {border:solid 2px #f50;}
Validator = {
	Require : /\s*\S+\s*/,
	RemarkLength : /^\s*\S{0,500}\s*$/,
	RegExpValidate:"this.RegExpValidate(value,accessKey)",
	TextLength : "this.CheckTextLength(value,accessKey)",
	Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
	EmailOrNull : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
	Birthday : /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/,
	BirthdayOrNull : /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/, 
	Phone : /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/,
	PhoneOrNull : /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/,
	LocalPhone : /(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/,
	LocalPhoneOrNull : /(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/,
	Mobile : /^1(3[0-9]|8[5-9]|5[01256789])\d{8}$/,
	MobileOrNull : /^1(3[0-9]|8[5-9]|5[01256789])\d{8}$/,
	Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
	UrlOrNull : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
	IdCard : "this.IsIdCard(value)",
	IdCardOrNull : "this.IsIdCard(value)",
	Currency : /^\d+(\.\d+)?$/,
	Number : /^\d+$/,
	NumberOrNull : /^\d*$/,
	Zip : /^[1-9]\d{5}$/,
	ZipOrNull : /^[1-9]\d{5}$/,
	QQ : /^[1-9]\d{4,15}$/,
	QQOrNull : /^[1-9]\d{4,15}$/,
	Integer : /^[-\+]?\d+$/,
	IntegerAndNotMinus : /^[+]?\d+$/,   //非负整数
	IntegerOrNullAndNotMinus : /^[+]?\d*$/,   //可以是空或者非负整数
	IntegerOrNull : /^[-\+]?\d*$/,  //可以是空或者必须是整数
	Double : /^[-\+]?\d+(\.\d+)?$/,
	DoubleAndNotMinus : /^[+]?\d+(\.\d+)?$/,   //非负整数、小数
	DoubleOrNull : /^[-\+]?\d*(\.\d+)*$/,  // 可以是空或者必须是整数、小数
	English : /^[A-Za-z]+$/,
	EnglishOrNull : /^[A-Za-z]*$/,
	BigWord : /^[A-Z]+$/,
	BigWordOrNull : /^[A-Z]*$/,
	Chinese : /^[\u0391-\uFFE5]+$/,
	ChineseOrNull : /^[\u0391-\uFFE5]*$/,
	UnChinese : /^\w+$/,// 必填,由数字、26个英文字母或者下划线组成的字符串
	UnChineseOrNull : /^\w*$/,// 必填,由数字、26个英文字母或者下划线组成的字符串
	JpgImage : /(.jpg)$/, //用于验证图片扩展名的正则表达式(.jpg|.png|.gif|.ps|.jpeg)
	xlsFile : /(.xls)$/, 
	Username : /^[a-z]\w{3,}$/i,
	UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,
	Year : /^[0-9]\d{3}$/,
	Month : "this.IsMonth(value)",
	IsSafe : function(str) {
		return !this.UnSafe.test(str);
	},
	SafeString : "this.IsSafe(value)",
	Filter : "this.DoFilter(value, getAttribute('accept'))",
	Limit : "this.limit(value.length,getAttribute('min'),  getAttribute('max'))",
	LimitB : "this.limit(this.LenB(value), getAttribute('min'), getAttribute('max'))",
	Date : "this.IsDate(value, getAttribute('min'), getAttribute('format'))",
	Repeat : "value == document.getElementsByName(getAttribute('to'))[0].value",
	Range : "getAttribute('accesskey').split(',')[0] <= (value|0) && (value|0) <= getAttribute('accesskey').split(',')[1]",
	Compare : "this.compare(value,getAttribute('operator'),getAttribute('to'))",
	Custom : "this.Exec(value, getAttribute('regexp'))",
	Group : "this.MustChecked(getAttribute('name'), getAttribute('min'), getAttribute('max'))",
	ErrorItem : [document.forms[0]],
	CellValidator : "cellCheck(value,getAttribute('index'))",
	ErrorMessage : [""],
	
	//错误消息元素后缀
	ErrorMsg : "ErrorMsg",
	//系统默认边框颜色
	DefaultColor : "#00A8E6",
	//输入错误边框颜色
	ErrorColor : "#FF3300",
	//输入正确边框颜色
	RightColor : "#00CC00",
	
	/** 消息占位符 **/
	replaceOne : "",
	replaceTwo : "",
	replaceThree : "",
	
	Validate : function(theForm) {
		var obj = theForm |