Javascript parseInt 总是返回 NaN?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10184368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:13:20  来源:igfitidea点击:

parseInt always returns NaN?

javascriptnanparseint

提问by somdow

long story short, i was trying to validate a phone field. ive added the isNaNand parseIntfor checking the " "in the field but that said

长话短说,我试图验证电话字段。我添加了isNaNparseInt用于" "在现场检查但那说

This below never validates to true..what am i missing?

下面的这个永远不会验证为真的..我错过了什么?

if(isNaN(parseInt(phone))){
        error.text("Sorry but this phone field requires numbers only");
        return false;
    } else {
    return true;

    }

it always fails...it never reads true even when i enter a number in the field and submit. i always get the error mssg.

它总是失败......即使我在字段中输入一个数字并提交,它也永远不会读为真。我总是收到错误消息。

EDIT:I am testing input values from a form, phoneis the name of the field.

编辑:我正在测试表单中的输入值,phone是字段的名称。

回答by Phrogz

Various ways to coerse JS strings to numbers, and their consequences:

将 JS 字符串强制转换为数字的各种方法及其后果:

Results of converting various strings using the above techniques
(source: phrogz.net)

Results of converting various strings using the above techniques
(来源:phrogz.net

I personally use *1as it is short to type, but still stands out (unlike the unary +), and either gives me what the user typed or fails completely. I only use parseInt()when I knowthat there will be non-numeric content at the end to ignore, or when I need to parse a non-base-10 string.

我个人使用*1它,因为它打字很短,但仍然很突出(与一元 + 不同),要么给我用户输入的内容,要么完全失败。我只parseInt()在我知道最后会忽略非数字内容时使用,或者当我需要解析非基数为 10 的字符串时。

Edit: Based on your comment, if using phone.val()fixed it then

编辑:根据您的评论,如果使用phone.val()修复它然后

  1. You were using jQuery (which you never mentioned, and should have), and
  2. You actually had/have a jQuery object, wrapping one or more DOM elements (probably just one).
  1. 您正在使用 jQuery(您从未提到过,并且应该使用过),并且
  2. 您实际上拥有/拥有一个 jQuery 对象,它包装了一个或多个 DOM 元素(可能只有一个)。

Whenever you do var foo = $('…');then the foovariable references a jQuery object of one or more elements. You can get the first actual DOM element from this via var fooEl = foo[0];or var fooEl = foo.get(0);…but even then you stillhave a DOM element and not a particular property of that.

每当您这样做时,var foo = $('…');foo变量都会引用一个或多个元素的 jQuery 对象。您可以通过var fooEl = foo[0];or从中获得第一个实际的 DOM 元素var fooEl = foo.get(0);……但即便如此,您仍然拥有一个 DOM 元素,而不是它的特定属性。

For form inputs, you need to get the .valuefrom the DOM element, which is what the jQuery .val()method does.

对于表单输入,您需要.value从 DOM 元素中获取 ,这正是 jQuery.val()方法所做的。

回答by rfunduk

parseIntis a bit odd at times:

parseInt有时有点奇怪:

> parseInt("123-456-789")
123

Fortunately you can probably solve your case with:

幸运的是,您可以通过以下方式解决您的问题:

> Number("123-456-789")
NaN

回答by Robert Harvey

parseInt only returns NaN if the first character cannot be converted to a number.

parseInt 仅在第一个字符无法转换为数字时返回 NaN。

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt

回答by Aaron Plocharczyk

I've seen Number()suggested, but that will still allow things like -21or 123.456. The best way to check for the absence of non-digits in a string is like this:

我已经看到了Number()建议,但这仍然允许像-21or 之类的东西123.456。检查字符串中是否缺少非数字的最佳方法是这样的:

function hasNonDigit(str){
  return /\D/g.test(str.toString());
}

console.log(hasNonDigit("123-456-7890"));
console.log(hasNonDigit("1234567890"));