简体   繁体   English

jQuery验证插件和自定义规则

[英]JQuery validation plugin and custom rule

I'm using the jquery.validation plugin in my web app. 我在Web应用程序中使用jquery.validation插件。 I have this general code for form validation: 我有以下用于表单验证的通用代码:

$('form[data-toggle="validator"]').each(function () {
            $(this).validate({
                errorClass: "help-block error",
                errorElement: "div",
                focusCleanup: false,
                focusInvalid: false,
                submitHandler: function(form){
                    $("[type=submit]",form).prop("disabled",true);
                    form.submit();
                }
            });
         });

When I need the validation I just set the data-toggle attribute to the form. 当我需要验证时,我只需将data-toggle属性设置为表单即可。

In a particular case I would like to add a custom rule, not for a single input element, but for all the form. 在特定情况下,我想添加一个自定义规则,而不是针对单个输入元素,而是针对所有表单。 I would like to add it at runtime, is it possibile? 我想在运行时添加它,可能吗?

I think you are searching for method to add a custom validation method. 我认为您正在寻找添加自定义验证方法的方法。 Below is the example where I add a validation to check for max length for a value at runtime. 下面是添加验证以在运行时检查值的最大长度的示例。

jQuery.validator.addMethod("maxLengthName", function(value, element) {
  return value.length>30;
}, 'Please enter a name with length less than 30.');

value :- the current value of the validated element 值:-已验证元素的当前值
element :- the element to be validated element:-要验证的元素

Update 更新资料

The above way is to add a custom method for a single input element. 上面的方法是为单个输入元素添加自定义方法。 Now the question remains how can we do same for form and not just 1 input element. 现在的问题仍然是,我们如何才能对表单(而不只是1个输入元素)执行相同的操作。

No, we cannot add a custom rule to the <form> element 不可以,我们不能向<form>元素添加自定义规则

But there is a way you can add a single rule for multiple inputs. 但是有一种方法可以为多个输入添加单个规则。 Below is the example for same. 下面是相同的示例。 In this example we check if addition of Input 1 and Input 2 is equal to Total entered by user 在此示例中,我们检查Input 1Input 2相加是否等于用户输入的Total

<form id="TotalForm" name="TotalForm">
    <label>Input 1</label>
    <input type="text" name="inputone" id="inputone" class="required" />
    <label>Input 2</label>
    <input type="text" name="inputTwo" id="inputTwo" class="required" />
    <label>Total</label>
    <input type="text" name="inputTotal" id="inputTotal" class="required totalMatch" />
</form>


jQuery.validator.addMethod("totalMatch",function(value) {
        total = parseFloat($('#inputone').val()) + parseFloat($('#inputTwo').val());
        return total == parseFloat($('#inputTotal').val());
    }, "Total is not matching with Input 1 and  Input 2!");

    jQuery.validator.classRuleSettings.totalMatch = { totalMatch: true };

Now you can call validate on document.ready function like below and 现在,您可以像下面这样在document.ready函数上调用validate,

$("#TotalForm").validate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM