Nou*_*ghT 2 kendo-ui kendo-validator
我想我有 2 个文本字段。我可以根据文本字段更改错误消息吗?
这是一个例子。
第一个文本框是电子邮件字段,并且是必填字段。所以我将错误消息保存在数组中:
["this field is required", "enter a valid e mail"]
Run Code Online (Sandbox Code Playgroud)
2 文本框为必填字段。它的错误消息:
["enter a valid value in the field"]
Run Code Online (Sandbox Code Playgroud)
但在像这样的kendo ui验证器中......
var validator = $("#formID").kendoValidator(
{ messages : {
required : "this field is required",
email : "enter a valid email address"
}
}).data("kendoValidator");
Run Code Online (Sandbox Code Playgroud)
如何根据文本字段错误消息动态更改这些值?
您可以function为两者创建required并根据 html 输入字段自定义错误消息email。
<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>
Run Code Online (Sandbox Code Playgroud)
脚本部分:
<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>
Run Code Online (Sandbox Code Playgroud)
在getRequiredMessage函数中,我根据输入名称自定义了错误消息。
我为输入用户名提供了“需要用户名”。如果您愿意,您甚至可以从数组提供错误消息。
| 归档时间: |
|
| 查看次数: |
8844 次 |
| 最近记录: |