Jquery 不引人注目的自定义错误消息
我创建了一个自定义 jquery 验证规则来检查十六进制颜色:
$.validator.addMethod("hexcolor", function (value, element) {
return this.optional(element) || /^(#){1}([a-fA-F0-9]){6}$/.test(value);
}, "Please insert a valid hex color (#______)");
$.validator.unobtrusive.adapters.add("hexcolor", function (options) {
options.rules['hexcolor'] = options.params;
options.messages['hexcolor'] = options.message;
});
当我创建文本框时,我有这样的问题
@Html.TextBoxFor(p => p.Color, new { data_val = "true", data_val_hexcolor = "Error message" })
我有两个问题:
当值无效时,错误消息是“错误消息”而不是“请插入有效的十六进制颜色 (#_ _ _ _ _ _)”
我在其他一些示例中看到过这样创建的错误消息。
jQuery.validator.addMethod("maxWords", function (value, element, params) { 返回 this.可选(元素) || stripHtml(value).match(/\b\w+\b/g).length <参数; }, jQuery.validator.format("请输入 {0} 个单词或更少。"));
字符串占位符“{0} {1}”等是否替换为参数值?
I created a custom jquery validation rule which checks for hex color:
$.validator.addMethod("hexcolor", function (value, element) {
return this.optional(element) || /^(#){1}([a-fA-F0-9]){6}$/.test(value);
}, "Please insert a valid hex color (#______)");
$.validator.unobtrusive.adapters.add("hexcolor", function (options) {
options.rules['hexcolor'] = options.params;
options.messages['hexcolor'] = options.message;
});
When i create my textbox, i have something like this
@Html.TextBoxFor(p => p.Color, new { data_val = "true", data_val_hexcolor = "Error message" })
I have two problems/questions:
When the value is invalid, the error message is "Error message" instead of "Please insert a valid hex color (#_ _ _ _ _ _)"
I've seen in some other examples error messages created like this.
jQuery.validator.addMethod("maxWords", function (value, element, params) {
return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length < params;
}, jQuery.validator.format("Please enter {0} words or less."));
String placeholder "{0} {1}" etc are replaced with the values of the parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题对我来说不是很清楚。但是,如果您尝试编写接受参数的自定义错误消息。这是实现这一目标的方法:
Your question is not very clear to me. However, if you are trying to write custom error messages that accept parameters. This is how to achieve that: