Reputation: 275
I have a range high and a range low and I need to be able to display if the input number is within the acceptable range, or if it is higher or lower than the acceptable range.
Currently, my javascript code looks like this but I keep getting the message saying it is an invalid entry.
it needs to be able to handle decimal places as the high and low are based on user preference.
var highRange = 17.80;
var lowRange = 6.90;
var valueToCheck = (rText.value);
function checkRange() {
var msg = 'Invalid entry';
if ((valueToCheck > 0.00) && (valueToCheck <= lowRange)) { msg = 'LOW'; }
if ((valueToCheck > lowRange) && (valueToCheck <= highRange)) { msg = 'NORMAL'; }
if ((valueToCheck > highRange)) { msg = 'HIGH'; }
rangeValue = msg;
}
I just don't understand why it isn't working, lol.
Any help would be much appreciated :-)
Upvotes: 0
Views: 90
Reputation: 1
Use parseFloat to convert your string to a float.
var valueToCheck = parseFloat(rText.value);
Upvotes: 0
Reputation: 1829
Try this.
var highRange = 17.80;
var lowRange = 6.90;
function checkRange() {
var valueToCheck = document.getElementById('rText').value;
var msg = 'Invalid entry';
if ((valueToCheck > 0.00) && (valueToCheck <= lowRange)) { msg = 'LOW'; }
if ((valueToCheck > lowRange) && (valueToCheck <= highRange)) { msg = 'NORMAL'; }
if ((valueToCheck > highRange)) { msg = 'HIGH'; }
document.getElementById('rangeValue').innerHTML = msg;
}
<input type="text" id="rText" >
<input type="button" value="Validate" onclick="checkRange()">
<p id="rangeValue"></p>
valueToCheck
outside the function.. Therefore when the page is loaded it already gets the null value of the input..
The valueToCheck
must be inside the function in order to get what are the changes in the input..
Upvotes: 2