1. 程式人生 > >jQuery validate 動態修改自定義驗證方法的提示訊息

jQuery validate 動態修改自定義驗證方法的提示訊息

    有時後提示資訊要在執行了驗證方法的回撥之後才能確定該顯示什麼樣的資訊,這個時候用如下方法就不能達到要求:
        
$.addMethod('customFun',function(value, element, param){
     console.log('This is test');
if(confirm('test')){
returntrue;
}
returnfalse
},'custom error message');
這個時候需要在回撥內部更改訊息:
$.addMethod('customFun',function(value, element, param){
var customMsg ='';
var result    =true;
if(value.length 
<5){ customMsg ='長度太短'; result =false; }elseif(value.length >20){ customMsg ='長度太長'; result =false; } $.validator.messages.customFun = customMsg; return result; });
http://blog.163.com/zhygpy%40126/blog/static/665230752013018074284/