使用自定义 jQuery 验证类
我想用 jQuery 进行一些自定义验证。 jQuery 验证带有一个“必需”类,开箱即用。这意味着我可以有一个脚本声明
$('form').validate()
,并且下一个文本框将根据我添加到其中的“必需”类正确验证:
<input type="text" value="" name="flowstatus" id="flowstatus" class="required">
我的新验证规则是:文本框中的文本长度应在 15 到 50 个字符之间。有没有办法创建我自己的自定义规则/类来实现类似的东西
<input type="text" value="" name="flowstatus" id="flowstatus" class="from15to50">
注意:这不是我需要的:
('form').validate({
rules: {
flowstatus: {required: true, minlength: 15, maxlength: 50}
}});
最后一个脚本与我想要验证的字段耦合得太紧密。
I have some custom validation I'd like to do with jQuery. jQuery validation comes out of the box with a 'required' class. This means that I can have a script declaring
$('form').validate()
and the next textbox will validate correctly, based on the class 'required' I add to it:
<input type="text" value="" name="flowstatus" id="flowstatus" class="required">
My new validation rule is: the text in a textbox should be between 15 and 50 chars long. Is there a way to create my own custom rule/class to implement something like
<input type="text" value="" name="flowstatus" id="flowstatus" class="from15to50">
Note: This is NOT what I need:
('form').validate({
rules: {
flowstatus: {required: true, minlength: 15, maxlength: 50}
}});
This last script is too tightly coupled to the fields I want to validate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我非常确定您可以向所需元素添加一个属性来定义其最小和最大长度。就像:
但是,我不太确定自定义类解决方案。
I'm pretty sure that you can add an attribute to the desired element defining its min and max lengths. Something like:
I'm not quite sure about the custom class solution, however.