使用自定义 jQuery 验证类

发布于 2024-10-05 08:56:20 字数 640 浏览 0 评论 0原文

我想用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温折酒 2024-10-12 08:56:20

我非常确定您可以向所需元素添加一个属性来定义其最小和最大长度。就像:

<input type="text" value="" name="flowstatus" id="flowstatus" class="required" minlength="15" maxlength="50">

但是,我不太确定自定义类解决方案。

I'm pretty sure that you can add an attribute to the desired element defining its min and max lengths. Something like:

<input type="text" value="" name="flowstatus" id="flowstatus" class="required" minlength="15" maxlength="50">

I'm not quite sure about the custom class solution, however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文