简体   繁体   中英

Price comparisons for input validation

I have three input fields. Two are dynamic and holds numeric values of low price and high price while the third is an open input field that receives prices entered by the user. My aim is to ensure that the price entered into the third field falls within range of the low price and the high price and if it does not, the field should be cleared automatically and ready for new input while an error popup message is displayed to the user.

My problem is that the input validation does not allow numbers within this range and is preventing any price from being entered because the code does not implement the range rule. Can someone help me check what I am doing wrongly. I need for the valid input to be within range of the low price and high prices

My code below:

function pricerange()
{
var  lowprice=document.getElementbyId(low_price).value; 
var offerprice=document.getElementbyId(offer_price).value
var high_price=document.getElementbyId(high_price).value
if(parseFloat(low_price) <parseFloat(offer_price) || parseFloat(offer_price) >parseFloat(high_price) )
alert('not allowed');
}

My code to clear the field if input is invalid:

function EraseField(frm){
frm.offer_price.value = ""
}

My input fields:

<input type="text" id="lowp" name="lowp" value="<?= $min_price_range; ?>" />
<input id="offer_price" name="offer_price" onfocus="EraseField(this.form)" onkeyup="priceband() type="text" class="form-control">
<input type="text" id="highp" name="highp" value="<?= $max_price_range; ?>" />

I'm pretty sure you made your < & > signs backwards.
if you have these numbers of lowprice 5, offered of 8 and high price of 10 then your code would look like this
if 5 < 8 then (which is TRUE)
--don't allow code--
but 8 falls within your boundaries. Am I missing something?

Edit: I stated this in the comment, but I should clarify it here as well. This is a problem with your OR statements. OR statements run through all of the statements one by one, immediately when it encounters a successful OR statement, it will run the code. So if it is TRUE at the first statement, it WILL NOT read the rest of the if statement (unless it's a complex if statement, one with a bunch of logic rules), it will skip all else.

This is why your code is not running, first because your < and > signs are backwards, and your logic is flawed. When a number is above the lowest, great, it works, then it will never read the rest of the if statement . So when it marks as false to the first statement, it will then be read as TRUE from the second, and it will then proceed anyways. This is how all programming languages use or, refer to a boolean logic sheet.

To revise your code to make it usable and make it work for (what I believe you want, to make sure it falls inside of two boundaries) it should be written like this:

if(parseFloat(low_price) >= parseFloat(offer_price) || parseFloat(offer_price) >= parseFloat(high_price))
alert("NOT ALLOWED VALUES");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM