0

I see this code in the jQuery validation documentation and would like to implement it:

jQuery.validator.addMethod("math", function(value, element, params) {
        return this.optional(element) || value == params[0] + params[1];
    }, jQuery.format("Please enter the correct value for {0} + {1}"));

I have a form with 3 input fields. They look like this:

<input type="text" class="form-control id="DolAmt" name="DolAmt" placeholder="0.00">
<input type="text" class="form-control id="ForfAmt" name="ForfAmt" placeholder="0.00">
<input type="text" class="form-control id="DepAmt" name="DepAmt" placeholder="0.00">

I have the following validation:

<script>$(document).ready(function () {
        $("#Form1").validate({
            // Specify validation rules
            rules:
            {
                ForfAmt: {
                    number: true,
                    min: 0
                },
                DolAmt: {
                    required: true,
                    min: 0,
                    number: true
                },
                DepAmt: {
                    required: true,
                    min: 0,
                    number: true,
                    math: true
                },
            },

        });
    });

jQuery.validator.addMethod("math", function(value, element, params) {
  return this.optional(element) || value == params[0] + params[1];
}, jQuery.format("Please enter the correct value for {0} + {1}"));

$(document).ready(function() {
  $("#Form1").validate({
    // Specify validation rules
    rules: {
      ForfAmt: {
        number: true,
        min: 0
      },
      DolAmt: {
        required: true,
        min: 0,
        number: true
      },
      DepAmt: {
        required: true,
        min: 0,
        number: true,
        math: true
      },
    },

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js" integrity="sha512-37T7leoNS06R80c8Ulq7cdCDU5MNQBwlYoy1TX/WUsLFC2eYNqtKlV0QjH7r8JpG/S0GUMZwebnVFLPd6SU5yg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

I have a form with 3 input fields. They look like this:
<form>
<input type="text" class="form-control id="DolAmt" name="DolAmt" placeholder="0.00">
<input type="text" class="form-control id="ForfAmt" name="ForfAmt" placeholder="0.00">
<input type="text" class="form-control id="DepAmt" name="DepAmt" placeholder="0.00">
</form>

The validation in the function math needs to retrieve the values entered into DolAmt and ForfAmt, add them together, and compare the sum to DepAmt. If the values are equal, return true. If they are not equal, return false with an error.

I do not know how to get DepAmt.value and ForfAmt.value into the params of the "math" function.

Any help is greatly appreciated. Thanks!

Mark Schultheiss
  • 30,473
  • 11
  • 66
  • 95
Jonathan Small
  • 773
  • 3
  • 12
  • 31
  • FWIW I made a snippet of your code as posted; including the syntax errors and added references to jQuery and the (assumed) validator. IF the examples do NOT answer your question please update your question to explain why – Mark Schultheiss Feb 15 '22 at 22:10

0 Answers0