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

javascript校验实例三
/**
 * 获取id获取name,className选择器获取对象;
 * @param options
 */
CheckOut.getField = function (options){
	this.field = null;
	if(options.id!=null && options.id!="" && options.id!=undefined){
		this.field = jQuery("#"+options.id);
	}
	if(options.name!=null && options.name!="" && options.name!=undefined){
		this.field = jQuery("*[name='"+options.name+"']");
	}
	if(options.className !=null && options.className!="" && options.className!=undefined){
		this.field = jQuery("."+options.className);
	}
	return this.field;	
};

/**
 * 初始化默认的验证类型;
 * @param options
 */
CheckOut.validateType = function(){
	//添加验证类型方法;
	this.addValidateType=function(validateEntity){
		validTypeArray.push(validateEntity);
	};
	
	//检验是否是数字;
	this.addValidateType(new ValidateEntity("CHECKOUT_NUMBER",function(value,param){
		var paraArray =new CheckOut.ParseArray(param);
		var paramArray = paraArray.getArrayParam();
		var textVal = value.val();
		var result = false;
		if(textVal.length>=parseInt(paramArray[0]) && textVal.length<= parseInt(paramArray[1])){
			var re = /^([0-9])+$/;
			result =  re.test(value.val());
		}
		CheckOut.setImagePosition(value,result,null);
		return result;
	}));
	
	//检验长度;
	this.addValidateType(new ValidateEntity("CHECKOUT_LENGTH_RANGE",function(value,param){
		var paraArray =new CheckOut.ParseArray(param);
		var paramArray = paraArray.getArrayParam();
		var txtValue =  value.val();
		if(txtValue.length>=parseInt(paramArray[0]) && txtValue.length <=parseInt(paramArray[1])){
			CheckOut.setImagePosition(value,true,null);
			return true;
		}else{
			CheckOut.setImagePosition(value,false,null);
			return false;
		}
	}));
	
	//检验手机号码;
	this.addValidateType(new ValidateEntity("CHECKOUT_MOBILE_NUMBER",function(value,param){
		var txtValue =  value.val();
		var expression = /^13[0-9]{1}[0-9]{8}$|^15[012356789]{1}[0-9]{8}$|^18[0256789]{1}[0-9]{8}$/;
		
		var result =  txtValue.length==11 && expression.test(txtValue);
		CheckOut.setImagePosition(value,result,null);
		return result;
	}));
	
	//获取validTypeArray验证数组;
	this.getValidTypeArray = function(){
		return validTypeArray;
	};
	
	//根据key获取验证类型;
	this.getValidTypeByKey = function(key){
		if(key!=null && key!="" && key != undefined){
			var parse =  new CheckOut.ParseArray(key);
			key = parse.getValidateTypeName();
			for(var i = 0;i<validTypeArray.length;i++){
				if(key==validTypeArray[i].getKey){
					return validTypeArray[i];
				}
			}
		}
		return null;
	};
};

?