【发布时间】:2019-09-02 14:42:07
【问题描述】:
我正在使用 Jquery valdiation plugin 在客户端验证表单。但我想做这样的验证。
<input type="text" id="txtInf" regex="/some regular expression/" error="Inf is mandatory"></inf>
这里 regex 和 error 是自定义属性。字段将根据正则表达式中的给定正则表达式进行验证,如果正则表达式文本失败,则应显示错误消息。
我尝试像这样向 validator 添加一个方法。
$("*[regex]").each(function () {
$.validator.addMethod($(this).attr('id'), function () {
return this.optional($(this)) || $(this).attr('regex').test($(this).text());
}, $(this).attr('error'));
});
但是这种方法存在一些问题。请让我知道,如果我认为它是正确的。 如果您有其他想法,请告诉我。欢迎任何思考过程。
【问题讨论】:
标签: javascript jquery validation