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

JS验证类

刚刚写的一个JS验证类

//验证静态方法
Validate.validate = function(){
	var eleArray = document.getElementsByTagName("*"); 
	var i;
	for (i = 0; i < eleArray.length; i++){
		if (eleArray[i].type == 'text'){
				var v = new Validate();
				if (v.validateTest(eleArray[i]) == false){
					return false;
				}
		}
		if (eleArray[i].type == 'select-one'){
				var v = new Validate();
				if (v.validateSelect(eleArray[i]) == false){
					return false;
				}
		}
		if (eleArray[i].type == 'textarea'){
				var v = new Validate();
				if (v.validateTextarea(eleArray[i]) == false){
					return false;
				}
		}
		if (eleArray[i].type == 'radio'){
			var v = new Validate();
			if (v.validateRadio(eleArray[i]) == false){
				return false;
			}
		}
		if (eleArray[i].type == 'checkbox'){
			var v = new Validate();
			if (v.validateCheck(eleArray[i]) == false){
				return false;
			}
		}
	}
	return true;
}


//验证类
function Validate(){
	this.INTEGER = 'integer';  //整数
	this.FLOAT = 'float';  //小数
	this.EMAIL = 'email';  //email
	this.URL = 'url';  //url
	this.PHONENUM = 'phonenum';  //电话号码座机 0511-4405222 或 010-87888822
	this.POSTAL = 'postal';  //邮编
	this.IDCARD = 'idcard';  //身份证
	this.IP = 'ip';  //IP
	
	//验证text
	this.validateTest = function(obj){
		var vu = new ValidateUtil();
		//验证是否为空
		if (obj.attributes.isNull != null){
			if (obj.attributes.isNull.value == 'false'){
				if (vu.isNull(obj) == false){
					return false;
				}
			}
		}
		//验证数据类型
		if (obj.attributes.dataType != null){、
			//整数
			if (obj.attributes.dataType.value == this.INTEGER){
				 if (vu.isInteger(obj) == false){
					 return false;
				 }
			}
			//小数
			if (obj.attributes.dataType.value == this.FLOAT){
				 if (vu.isFloat(obj) == false){
					 return false;
				 }	 
			}
			//email
			if (obj.attributes.dataType.value == this.EMAIL){
				var strP = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
				if (vu.validateDataType(obj, strP) == false){
			 		return false;
				}
			}
			//url
			if (obj.attributes.dataType.value == this.URL){
				//var strP=/^(http\:\/\/)?([\w.]+)(\/[\w-\.\/\?%&=]*)?$/;
				//if (vu.validateDataType(obj, strP) == false){
				// 		return false;
				//}
			}
			//电话号码座机 0511-4405222 或 010-87888822
			if (obj.attributes.dataType.value == this.PHONENUM){
				var strP = /^\d{3}-\d{8}|\d{4}-\d{7}$/;
				if (vu.validateDataType(obj, strP) == false){
			 		return false;
				}
			}
			//邮编
			if (obj.attributes.dataType.value == this.POSTAL){
				var strP = /^[1-9]\d{5}(?!\d)$/;
				if (vu.validateDataType(obj, strP) == false){
			 		return false;
				}
			}
			//身份证
			if (obj.attributes.dataType.value == this.IDCARD){
				var strP = /^\d{15}|\d{18}$/;
				if (vu.validateDataType(obj, strP) == false){
			 		return false;
				}
			}
			//IP
			if (obj.attributes.dataType.value == this.IP){
				var strP = /^\d+\.\d+\.\d+\.\d+$/;
				if (vu.validateDataType(obj, strP) == false){
			 		return false;
				}
			}
		}
		return true;
	}
	
	//验证textarea
	this.validateTextarea = function(obj){
		var vu = new ValidateUtil();
		if (obj.attributes.isNull != null){
			if (obj.attributes.isNull.value == 'false'){
				return vu.isNull(obj);	
			}
		}
	}
	
	//验证select
	this.validateSelect = function(obj){
		var vu = new ValidateUtil();
		if (obj.attributes.isNull != null){
			if (obj.attributes.isNull.value == 'false'){
				return vu.isNull(obj);	
			}
		}
	}
	
	//验证radio
	this.validateRadio = function(obj){
		var vu = new ValidateUtil();
		if (obj.attributes.isNull != null){
			if (obj.attributes.isNull.value == 'false'){
				var radioObj = document.getElementsByName(obj.attributes.name.value);
				return vu.isNullRadio_CheckBox(radioObj);
			}
		}
	}
	//验证checkbox
	this.validateCheck = function(obj){
		var vu = new ValidateUtil();
		if (obj.attributes.isNull != null){
			if (obj.attributes.isNull.value == 'false'){
				var boxObj = document.getElementsByName(obj.attributes.name.value);
				//alert(vu.isNullR