星期六, 11月 26, 2011

[jQuery Plugin] jQuery Validator: Dynamic message

剛好有需求要動態變更錯誤提示訊息,以下範例是目前的寫法請參考, 關鍵的行數為this.settings.messages[element.name]



$.validator.addMethod( "passwordrule" , function(value,element){
  var re =   /^[a-zA-Z0-9@%\\+\/'!#$\^\?:\(\)\{\}\[\]~\-_]+$/g;
  var searchStr = value;
  var reElse = /[^a-zA-Z0-9@%\\+\/'!#$\^\?:\(\)\{\}\[\]~\-_]{1}/g;
  var result = searchStr.match(reElse);
  if(result != null){
   //console.log("invalid chars:" + result.toString());
   //dynamic setting message
this.settings.messages[element.name].passwordrule = $.printf(global_validate_only_passwordrule,[result.toString()]);
  }
  return value.match(re);
});
之前忘記加上passwordrule( this.settings.messages[element.name]),會導致所有錯誤訊息都會變成一樣的 this.settings.messages[element.name] = $.printf(global_validate_only_passwordrule,[result.toString()]);

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails