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!