jquery-validate - addMethod - how to apply custom rule referencing two text boxes?

前端 未结 1 1545
耶瑟儿~
耶瑟儿~ 2020-12-13 20:46

I\'m using the jQuery validation plugin and I want make use of a custom method which takes two the values entered into two different textboxes into account when deciding if

相关标签:
1条回答
  • 2020-12-13 20:57

    Try the following:

    Create the Rule

    jQuery.validator.addMethod("checkTotal",function(value) {
        total = parseFloat($('#LHSOPERAND').val()) + parseFloat($('#RHSOPERAND').val());
        return total == parseFloat($('#TOTAL').val());
    }, "Amounts do not add up!");
    
    jQuery.validator.classRuleSettings.checkTotal = { checkTotal: true };
    

    Input

    <input type="text" name="TOTAL" id="TOTAL" class="required checkTotal" />
    

    Init Validation

    $(document).ready(function() {
        $("#TOTALSFORM").validate();
    });
    

    Fiddle here: http://jsfiddle.net/wTMv3/

    0 讨论(0)
提交回复
热议问题