First of all, the isNaN function does NOT REALLY check if a string is a number. For example, isNaN ('456a') returns true, but '456a' is not a number. For this you need a different verification method. I would suggest using regular expressions.
Then you need to parse the string to compare numbers (i.e. price_from <price_to).
Here is a modified code that you can assume:
$('.filter-price').submit(function(e) { var alert_message = ''; var price_from = $('.filter-price #price_from').val(); var price_to = $('.filter-price #price_to').val(); var isNumberRegExp = new RegExp(/^[-+]?[0-9]+(\.[0-9]+)*$/); if (!isNumberRegExp.test(price_from) || !isNumberRegExp.test(price_to)) { if (!isNumberRegExp.test(price_from)) { alert_message += "Price from must be a number, ie 500\n"; $('.filter-price #price_from').val('From'); } if (!isNumberRegExp.test(price_to)) { alert_message += "Price to must be a number, ie 500\n"; $('.filter-price #price_to').val('To'); } } else { price_from = parseFloat(price_from); price_to = parseFloat(price_to); if (price_from >= price_to) { alert_message += "Price from must be less than price to\n"; $('.filter-price #price_from').val('From'); $('.filter-price #price_to').val('To'); } } if (alert_message != '') { e.preventDefault(); alert(alert_message); } });
source share