当前位置: 代码迷 >> Ajax >> ajax 印证 校验 插件
  详细解决方案

ajax 印证 校验 插件

热度:602   发布时间:2012-07-15 20:11:38.0
ajax 验证 校验 插件

?

/**
 * ajaxValidate 服务端验证
 * param {url:'必填',data:'传递的数据,为空:则自动绑定当前值,非空:则合并上当前值',若为字符串:则直接拼接到url,before:前置方法,after:后置方法}
 * 服务端返回格式 {"rs":true,"msg":"","datas":{}}  可直接调用controller中的: this.outPrintSuccess(response,"")  || this.outPrintFail(response, "占用");
 * messages 中如果未填写 ajaxValidate:'' 属性,则错误信息为服务端返回的this.outPrintFail(response, "占用") 的提示信息 "占用"
 * @author wjs 20120329
 */
jQuery.validator.addMethod("ajaxValidate", function(value, element, param) {
	//url 验证
	if(typeof(param.url)  == 'undefined' || param.url == null || $.trim(param.url) == '') {
		alert("参数URL为空");
		return false;
	}
	
	//前置方法接口 参数为validator对象
	if(typeof(param.before) == 'function') {
		if(!param.before(this)){
			return true;
		}
	}
	
	//将url+上时间搓
	var url = $.trim(param.url + "");
	while(url.charAt(url.length-1) == '/'){
		url = url.substring(0,url.length-2);
	}
	if(url.indexOf("?") == -1){
		url += "?"+new Date().getTime();
	}else{
		url += "&"+new Date().getTime();
	}
	
	//将用户参数传递到服务端,并+入被验证字段的属性值
	var data = {};
	if(typeof(param.data) == 'undefined'){
		data[$(element).attr("name")] = $.trim($(element).val());
	}
	
	if(typeof(param.data) == 'object'){
		data = param.data;
		if(typeof(data[$(element).attr("name")]) == "undefined"){
			data[$(element).attr("name")] = $.trim($(element).val());
		}
	}
	
	//数据位字符串类型
	if(typeof(param.data) == 'string'){
		var pData = $.trim(param.data);
		url += "&"+pData+"&" + $(element).attr("name") + "=" + $.trim($(element).val());
	}
	
	//和前一个进行比较,如果值相同,则不发送ajax验证,直接将上一个提示信息显示出来
	var validator = this;
	var previous = this.previousValue(element);
	if (!this.settings.messages[element.name] )
		this.settings.messages[element.name] = {};
	previous.originalMessage = this.settings.messages[element.name].ajaxValidate;
	this.settings.messages[element.name].ajaxValidate = previous.originalMessage;

	if ( this.pending[element.name] ) {
		return "pending";
	}
	if ( previous.old === value ) {
		return previous.valid;
	}
	
	previous.old = value;
	this.startRequest(element);
	$.ajax({
		url : url,
		type : 'post',
		data : data,
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		dataType: 'json',
		timeout: 10000,
	    error: function(){
	        alert('验证出现错误');
	        return false;
	    },
	    success: function(obj){
	    	if(typeof(param.after) == 'function') {
				param.after(validator,obj);
			}
			validator.settings.messages[element.name].ajaxValidate = previous.originalMessage;
			if(obj.rs){
				var submitted = validator.formSubmitted;
				validator.prepareElement(element);
				validator.formSubmitted = submitted;
				validator.successList.push(element);
				//清空错误信息
				validator.showErrors();
			}else{
				var errors = {};
				var message = validator.settings.messages[element.name].ajaxValidate || obj.msg;
				errors[element.name] = message;
				//设置错误提示信息
				validator.showErrors(errors);
			}
			previous.valid = obj.rs;
			validator.stopRequest(element, obj.rs);
	    	return obj.rs;
	    }
	});
}, "远程验证");
check: 加入如下代码,在ajax验证未返回时都将无法提交
if (method == 'ajaxValidate'){
	var ems = $(element).parent().children("em");
	if(ems.length > 0){
		if(ems[0].style.display != "none"){
		        result = false;
		}
	}
}
  相关解决方案