如何使用github.com/1000hz/bootstrap-validator的自定义验证器 [英] How to use custom validators of github.com/1000hz/bootstrap-validator
问题描述
从文档 http://1000hz.github.io/bootstrap-validator/:
添加要运行的自定义验证器。验证器应该是接收jQuery元素作为参数的函数,并根据输入的有效性返回一个truthy或falsy值。
Add custom validators to be run. Validators should be functions that receive the jQuery element as an argument and return a truthy or falsy value based on the validity of the input.
对象结构是: {foo:function($ el){return true || false}}
将输入的验证器添加到其他输入中, data-foo =bar
。
Adding the validator to an input is done just like the others, data-foo="bar"
.
您还必须通过errors选项为任何自定义验证器添加默认错误消息。
You must also add default error messages for any custom validators via the errors option.
我不太明白如何定义我自己的自定义验证器以及如何在此插件中使用它。
I don't quite understand how to define my own custom validator and how to use it with this plugin.
任何人都可以给我一个简单的例子或提示吗?
Could anyone give me a simple example or hint?
推荐答案
你需要手动调用你的插件,如自定义
选项不适用于数据属性:
You need to call your plugin manually, as custom
options will not work with data-attributes:
$().validator({
custom: {
'odd': function($el) { return Boolean($el.val() % 2);}
}
})
然后像这样使用它:
<input placeholder="plz enter odd value" data-odd>
不要忘记添加错误消息,查看代码
Don't forget to add error messages, see code
这篇关于如何使用github.com/1000hz/bootstrap-validator的自定义验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!