Overlugged
Overlugged

Reputation: 39

Javascript isNaN() function is ignored

I'm trying to develop a little finance page where you put the price of your object, the upfront deposit you can afford and the number of years you want to pay, and when you submit, it tells you how much you're going to pay per months.

The problem I have is that if someone puts letters in one of the fields, the process is still done.

In an ideal universe I would like to put in the result field "Please put only numbers" but my isNaN test is completely ignored. For the moment, when you put letters in the deposit field for instance it simply considers it as a 0.

How do i get it working ?

Here's my Fiddle : http://jsfiddle.net/overplugged/zq5wE/2/

if (!isNaN(deposit))
{
    $("#result").val(result);
}   
else{

    alert("Must input numbers");
    return false;
}

And this is where my problem is

Upvotes: 0

Views: 77

Answers (1)

Matas Vaitkevicius
Matas Vaitkevicius

Reputation: 61409

you might want to use regex to check for numeric input

var regex = new RegExp('^[0-9]+$'); // for decimal point new RegExp('(?:^|(?<=\s))[0-9]*\.?[0-9](?=\s|$)');
if(regex.test(deposit)) 
{
    $("#result").val(result);
}   
else
{
    alert("Must input numbers");
    return false;
}

Upvotes: 1

Related Questions