最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript isNaN() function not working? - Stack Overflow

programmeradmin8浏览0评论

I have a function to test if a prompt input is a number, like so:

function myFunction() 
{
    var person = prompt("Please enter your name", "");

    if (person != null) 
    {
        if(isNaN(person))
        {
            document.write("hello " + person + "<br><br>");
        }
        else
            document.write("You gave me a number");

    }
    else
    {
        document.write("You didn't answer.<br><br>");
    }
}

but every time I enter a number it keeps outputting hello + the number. I've been googling this function for quite some time and it doesn't make sense to me, it seems like it should work. Why is person returning true?

I have a function to test if a prompt input is a number, like so:

function myFunction() 
{
    var person = prompt("Please enter your name", "");

    if (person != null) 
    {
        if(isNaN(person))
        {
            document.write("hello " + person + "<br><br>");
        }
        else
            document.write("You gave me a number");

    }
    else
    {
        document.write("You didn't answer.<br><br>");
    }
}

but every time I enter a number it keeps outputting hello + the number. I've been googling this function for quite some time and it doesn't make sense to me, it seems like it should work. Why is person returning true?

Share Improve this question asked Jan 27, 2016 at 21:11 23163546542316354654 2792 gold badges3 silver badges14 bronze badges 4
  • 1 Try replacing all your document.write calls with console.log and see what happens. Then read up on how document.write works. – j08691 Commented Jan 27, 2016 at 21:16
  • 1 I cannot reproduce this. What browser are you using? – George Simms Commented Jan 27, 2016 at 21:16
  • Just because it isn't NaN or null doesn't mean it's an integer... – War10ck Commented Jan 27, 2016 at 21:18
  • you're right @GeorgeSimms, this code doesn't produce an error. it wasn't my javascript code. I messed up the name of the javascript file in my html code >.< oops...... I guess the best answer would go to you? I'm not sure if I should close this thread or what.. – 2316354654 Commented Jan 27, 2016 at 23:29
Add a comment  | 

3 Answers 3

Reset to default 5

NaN is a special value in Javascript. What isNaN does is check to see if the value passed is equal* to this special value. If you want to check if something is, say, not a stream of numbers, you can use a regular expression:

if (!/^\d+(\.\d+)?/.exec(person)) {

Or parse the value as a number and see if it converts back to the same string:

var n = parseFloat(person);
if (n.toString() !== person) {

*There's a reason that we don't use === but it's outside the scope of this answer.

Your code is the correct way of using the isNaN method. However for anyone else reading this post I have seen a strange anomaly where the documented usage of iNaN hasn't worked properly and I got around the problem by combining the parseInt method with the isNaN method. According to w3schools the isNan('123') should return false and isNan('g12') should return true, but I've seen scenarios where this isn't the case. If you're having trouble getting the documented methods to work try this code below:

var unitsToAdd = parseInt($('#unitsToAdd').val());
if(isNaN(unitsToAdd)) {
    alert('not a number');
    $('#unitsToAdd').val('1');
    returnVal = false;
}

Alternatively you can try this method which is well tested.

function isNumber(searchValue) {
    var found = searchValue.search(/^(\d*\.?\d*)$/);
    //Change to ^(\d*\.?\d+)$ if you don't want the number to end with a . such as 2.
    //Currently validates .2, 0.2, 2.0 and 2.
    if(found > -1) {
        return true;
    }
    else {
        return false;
    }
}

The isNaN function checks if a value is NaN. NaN is a value that occurs when making operations that require numbers with non-numbers. Please see the documentation. However the function does not check if the value is of type number. Too check if a value is of type number use the typeof operator

typeof person === 'number'
发布评论

评论列表(0)

  1. 暂无评论