Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

price_from.toFixed() is not a function

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. 500\n";
            $('.filter-price #price_from').val('From');
        }

        if (isNaN(price_to))
        {
            alert_message += "Price to must be a number, i.e. 500\n";
            $('.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 to\n";
            $('.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.

like image 722
scarhand Avatar asked Nov 28 '22 18:11

scarhand


2 Answers

val returns a string; toFixed operates on numbers. Convert your string to a number like this:

Number(price_from).toFixed();

Note: You're checking whether the string contains a number using isNaN. This works because isNaN does an implicit number conversion before testing. To use any of the number methods, however, you'll need to do this same conversion explicitly, as shown above.

Don't confuse the JavaScript type of the object with what it represents. A string can contain a "number" in string form and still be just a string.

like image 89
Wayne Avatar answered Dec 05 '22 03:12

Wayne


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. 500\n";
            $('.filter-price #price_from').val('From');
        }

        if (!isNumberRegExp.test(price_to))
        {
            alert_message += "Price to must be a number, i.e. 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);
    }

});
like image 42
Engineer Avatar answered Dec 05 '22 03:12

Engineer