I'm using jQuery to compare the values of two input fields. One is the "maximum" value and one is the "minimum" value. The objective of the validation is to check if the minimum value is greater than the maximum value.
My logic is almost working but with a small bug. It is triggered with an onBlur event (on one of the tested fields) and once the pop-up alert displays, the user can click anywhere else, and the test is no longer run (thus allowing the minimum value to be greater than the maximum.
Here is the jQuery:
//Function to validate if maximum benefit value is more the minimum benefit value
function minMaxValues(){
var maxAmt = ($('#maxAmount').val());
var minAmt = ($('#minAmount').val());
if ((minAmt != '') && (maxAmt != '') && (maxAmt < minAmt)){
alert('The maximum benefit amount must be larger than the minimum amount.');
return false;
} else {
return true;
} //end maxAmt minAmt comparison
}//end minMaxValues function
The HTML:
<label>Maximum Benefit Amount:</label>
<input type="text" name="benefit_max_amount" value="" onBlur="minMaxValues();" id="maxAmount">
<label>Minimum Benefit Amount</label>
<input type="text" name="benefit_min_amount" value="" onBlur="minMaxValues();" id="minAmount">
How do I get jQuery to look at the page all the time and compare the values so that the condition can never be true without an alert (warning)?
EDIT: I should state that I'm looking for something modular, so that it can be used in other locations (thereby allowing this to go into an external js file, and not execute on page load, but only by calling a function.)