为 jquery validate 添加验证失败回调

转载自:https://blog.csdn.net/huang100qi/article/details/52619227

1. jquery Validation Plugin - v1.15.1 (此版本不支持IE8)

第240行左右:

if (!c) {
   return this.formatAndAdd(b, e), !1   
}

改成:

if (!c) {
   //return this.formatAndAdd(b, e), !1
   this.formatAndAdd(b, e);
   var errorCallback=this.settings.fail;
   if($.type(errorCallback)==='function'){
      errorCallback(b,e);
   }
   return false;
}

2. jquery Validation Plugin - v1.10.0 (此版本支持IE8)

第545行左右:

if( !result ) {
   this.formatAndAdd( element, rule );   
   return false;
}

改成:

if( !result ) {
   this.formatAndAdd( element, rule );
   var errorCallback=this.settings.fail;
   if($.type(errorCallback)==='function'){
      errorCallback(element);
   }
   return false;
}

好了,这下咱们就可以调用了

$("#forgetwordForm").validate({
    fail: function(element,e) {                
        //验证失败回调(扩展validate)
        var sce = $(element).next('label.success');
        sce.hide();
        var setTimeLater = setTimeout(function(){
            var error = $(element).siblings('label.error');
            if(error.find('.wrongIcon').length == 0) {
                error.prepend('<i class="wrongIcon"></i>');
            }
            clearTimeout(setTimeLater);
        },1);
    }
})

猜你喜欢

转载自www.cnblogs.com/mabaoqing/p/10315152.html