Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
186
rated 0 times [  187] [ 1]  / answers: 1 / hits: 16782  / 13 Years ago, mon, december 26, 2011, 12:00:00

pretty simple issue here, this was working before but not anymore.



heres my code:



$('.filter-price').submit(function(e) {

var alert_message = '';
var price_from = $('.filter-price #price_from').val();
var price_to = $('.filter-price #price_to').val();

if (isNaN(price_from) || isNaN(price_to))
{
if (isNaN(price_from))
{
alert_message += Price from must be a number, i.e. 500n;
$('.filter-price #price_from').val('From');
}

if (isNaN(price_to))
{
alert_message += Price to must be a number, i.e. 500n;
$('.filter-price #price_to').val('To');
}
}
else
{
price_from = price_from.toFixed();
price_to = price_to.toFixed();

if (price_from >= price_to)
{
alert_message += Price from must be less than price ton;
$('.filter-price #price_from').val('From');
$('.filter-price #price_to').val('To');
}
}

if (alert_message != '')
{
e.preventDefault();
alert(alert_message);
}

});


now web developer is giving me the error price_from.toFixed is not a function and my javascript is not working.


More From » jquery

 Answers
16

First of all 'isNaN' function does not REALLY check whether string represents number. For example isNaN('456a') returns true, but '456a' is not number at all. For this purpose you need other method of checking. I would suggest to use regular expressions.



Then you need to parse string for compareing numbers ( i.e. price_from < price_to ).



Here is the modificated code you may 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, i.e. 500n;
$('.filter-price #price_from').val('From');
}

if (!isNumberRegExp.test(price_to))
{
alert_message += Price to must be a number, i.e. 500n;
$('.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 ton;
$('.filter-price #price_from').val('From');
$('.filter-price #price_to').val('To');
}
}

if (alert_message != '')
{
e.preventDefault();
alert(alert_message);
}

});

[#88379] Friday, December 23, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alora

Total Points: 284
Total Questions: 99
Total Answers: 92

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;