【问题标题】:Dynamically change error messages in Kendo UI validator?在 Kendo UI 验证器中动态更改错误消息?
【发布时间】:2015-08-12 12:59:27
【问题描述】:

认为我有 2 个文本字段。我可以根据文本字段更改那里的错误消息吗?

这是一个例子。

第一个文本框是一个电子邮件字段,它是必填字段。 所以我把它保存在一个数组中:

["this field is required", "enter a valid e mail"]

2 文本框是必填字段。 它的错误信息:

["enter a valid value in the field"] 

但是在像这样的剑道用户界面验证器中......

  var validator = $("#formID").kendoValidator( 
       { messages : {
                     required : "this field is required",
                     email : "enter a valid email address"
       }
       }).data("kendoValidator"); 

如何根据文本字段错误消息动态更改这些值?

【问题讨论】:

    标签: kendo-ui kendo-validator


    【解决方案1】:

    您可以为requiredemail 创建function,以根据 html 输入字段自定义错误消息。

    <form id="myform">
        <input name="username" required /> <br />
        <input type="email" name="userEmail" required data-message="My custom email message" /> <br />
        <button>Validate</button>
    </form>
    

    脚本部分:

    <script>
         $("#myform").kendoValidator({
                 messages: {
                     // defines a message for the 'custom' validation rule
                     custom: "Please enter valid value for my custom rule",
    
                     // overrides the built-in message for the required rule
                     required: function(input) {
                         return getRequiredMessage(input);
                     },
    
                     // overrides the built-in message for the email rule
                     // with a custom function that returns the actual message
                     email: function(input) {
                         return getMessage(input);
                     }
                 },
                 rules: {
                   custom: function(input) {
                     if (input.is("[name=username]")) {
                         return input.val() === "Tom";
                     }
                     return true;
                   }
                 }
            });
    
            function getMessage(input) {
              return input.data("message");
            }
            function getRequiredMessage(input) {
               if (input.is("[name=username]")) {
                         return "User name is required";
                     }
                else
                     return input.attr("name") + " Field is Required";
            }
    </script>
    

    Jsfiddle

    getRequiredMessage 函数中,我根据输入名称自定义了错误消息。

    我为输入用户名提供了“需要用户名”。如果您愿意,甚至可以从数组中提供错误消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多