Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Can't get isNaN to work in my if statement

MonetishMonetish TexasMember Posts: 5

I can't get my isNaN to work... it seems to be recognizing characters as small numbers. (code below)

I have a feeling it may be because I've created an input box with a type="number"
so when that data is entered into my if statement variable it always makes it a number but..... maybe not.. thoughts.. here a few sections of my code if you can see what I'm doing wrong..(Everything else is working but when I check for user entry error of a character/string instead of a number it defaults to my first 'else if' response)

I've only entered the relative code...

html ..........

Enter your age: <input type="number" id="age" value=21>

<button onclick="checkAge()">Check Age</button>

...................(html stuff)..........(then script)..........

    var years = document.getElementById("age").value;

    if (isNaN(years)){
        var msg = "Invalid entry, not a number!";
    }

    else if (years<21) {
        var msg ="You are too YOUNG to enter the club!";
        document.getElementById("entry").innerHTML="";
    }

    else if (years>21) {........... etc.......

When I run a test to see if when I enter a name in the input box (EX: Amy)... instead of it giving me the message I wrote for isNaN ("Invalid...") it gives me the next else if ("You are too YOUNG...")

Thoughts? Do you see anything in the code I missed.

Thanks for your help or explanations to why this is happening. Monetish :-)

Comments

  • B00B00 Member Posts: 111 ✭✭✭

    Input type "number" is HTML5 feature and it allows only numbers as input. Browser does this check for you, no need for JS.
    Note: type="number" is not supported in IE9 and earlier.

  • David_BDavid_B Member Posts: 6 ✭✭

    I don't know if this is the best way to do it, but it is how I do it, and it works.
    I take in the number (as text by default) and then pass it to another (in the present example, an integer) variable via parseInt. Then, I do the NaN check.

    For example, let's call the initial input for the "years" variable "dummy_years." This usually comes from an HTML page as a string/text, so can't be used for math in its raw form.

    First, assign it to a dummy integer variable (dummy_Int) and do the test on it.

    After we have confirmed that it is a number, and an integer, then we'll assign the value to the actual "years" variable that will be used throughout the rest of the program for any tests, comparisons, or math operations.

    dummy_Int = parseInt(dummy_years); if (!isNaN(dummy_Int)){ years = dummy_Int; } else { alert("Invalid entry, not a number!"); }

  • MonetishMonetish TexasMember Posts: 5

    Thanks! That makes sense. I originally changed it to a type="text" and it worked. But I like this option to be able to keep the type="number". Thanks for the help!

Sign In or Register to comment.