jquery 验证器 addmethod 自定义消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/976384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:18:37  来源:igfitidea点击:

jquery validator addmethod custom message

jqueryvalidation

提问by timborden

I've created a method for jquery's validator plugin, that works like the remote rule. The difference is that I'd like to display a dynamic error message (based on the ajax response).

我为 jquery 的验证器插件创建了一个方法,它的工作原理类似于远程规则。不同之处在于我想显示动态错误消息(基于 ajax 响应)。

jQuery.validator.addMethod("duplicate", function(value, element, params) { 
    var object_settings = this.settings;
    params.data[$(element).attr("name")] = value;
    $.post(params.url, params.data, function(response) {
        if (response == 'true'){ return true; }
        else {
            object_settings.messages[element.name] = response;
            return false;
        }
    }, 'text');
}, '');

It works...sort of....it sets the message, but doesn't display it initially (if you validate the field a second time, the message is displayed).

它有效......有点......它设置消息,但最初不显示它(如果您再次验证该字段,则会显示该消息)。

Any suggestions?

有什么建议?

(maybe the remote rule offers this functionality...I couldn't find anything in the documentation)

(也许远程规则提供了这个功能......我在文档中找不到任何东西)

采纳答案by timborden

Here's the solution....needed to call the showErrors function of the object:

这是解决方案....需要调用对象的 showErrors 函数:

jQuery.validator.addMethod("duplicate", function(value, element, params) { 
    var validator = this;
    params.data[element.name] = value;
    $.post(params.url, params.data, function(response) {
        if (response == 'true'){ return true; }
        else {
            var errors = {};
            errors[element.name] =  response;
            validator.showErrors(errors);
            return false;
        }
    }, 'text');
}, '');

Taken from "remote" in jquery.validate.js (lines 917 - 919)

取自 jquery.validate.js 中的“remote”(第 917 - 919 行)

回答by jsherk

Was looking for solution to this as well, and found this...

也在寻找解决方案,并找到了这个......

In the original example, if you change this line:

在原始示例中,如果您更改此行:

object_settings.messages[element.name] = response;

To this:

对此:

$.validator.messages.duplicate = response;

This works for me. I found it here: http://blogs.teamb.com/craigstuntz/2009/01/15/37923/

这对我有用。我在这里找到它:http: //blogs.teamb.com/craigstuntz/2009/01/15/37923/

回答by Kiran

I have followed the process mentioned in the site http://blogs.teamb.com/craigstuntz/2009/01/15/37923/#comment-125774and succeeded.

我遵循了网站http://blogs.teamb.com/craigstuntz/2009/01/15/37923/#comment-125774中提到的过程并成功了。

You have to call the method with the dynamic message, so that it will display that message. For example

您必须使用动态消息调用该方法,以便它显示该消息。例如

$.validator.addMethod("validatePremium", function(value, element, param) {

    if( Condition )    

     {
       $.validator.messages.validatePremium = "your message here";
       //enter code here
       //...
       return false;
     }

    }, $.validator.messages.validatePremium);

回答by Tharindu

this worked for me

这对我有用

var errorMsg = '', $valid = false;
$.validator.addMethod("methodName",function(val, elem){
   $.ajax({
      url:'your_script.php',
      type:"POST",
      dataType:"json",
      data : {},
      success:function(response){
         if(response.success == false){
            errorMsg = response.msg;
            $valid = response.success;
         }
         else{
            $valid = true;
         }
      }
   });
   $.validator.messages["methodName"] = errorMsg;
   return $valid;
},'');

make sure to replace "methodName" with your method name in this case "duplicate" in both places(addMethod function 1st arg and in the addMethod function body $.validator.messages["methodName"])

在这种情况下,请确保在两个地方(addMethod 函数第一个参数和 addMethod 函数体 $.validator.messages["methodName"]) 中将“ methodName”替换为您的方法名称“重复”