gpt4 book ai didi

kendo-ui - 动态更改 Kendo UI 验证器中的错误消息?

转载 作者:行者123 更新时间:2023-12-02 15:23:58 26 4
gpt4 key购买 nike

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

这是一个例子。

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

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

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

["enter a valid value in the field"] 

但是在这样的剑道 ui 验证器中......

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

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

最佳答案

您可以为 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 函数中,我根据输入名称自定义了错误消息。

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

关于kendo-ui - 动态更改 Kendo UI 验证器中的错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31947675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com