Ele*_*hoy 1051 javascript validation numeric
我希望在旧的VB6 IsNumeric()
功能的同一个概念空间中有什么东西?
Dan*_*Dan 2110
无论变量包含的是字符串还是数字,这都有效.
isNaN(num) // returns true if the variable does NOT contain a valid number
Run Code Online (Sandbox Code Playgroud)
isNaN(123) // false
isNaN('123') // false
isNaN('1e10000') // false (This translates to Infinity, which is a number)
isNaN('foo') // true
isNaN('10px') // true
Run Code Online (Sandbox Code Playgroud)
当然,如果需要,你可以否定这一点.例如,要实现IsNumeric
您给出的示例:
function isNumeric(num){
return !isNaN(num)
}
Run Code Online (Sandbox Code Playgroud)
仅当字符串仅包含数字字符时才有效,否则返回NaN
.
+num // returns the numeric value of the string, or NaN
// if the string isn't purely numeric characters
Run Code Online (Sandbox Code Playgroud)
+'12' // 12
+'12.' // 12
+'12..' // Nan
+'.12' // 0.12
+'..12' // Nan
+'foo' // NaN
+'12px' // NaN
Run Code Online (Sandbox Code Playgroud)
用于将'12px'转换为12,例如:
parseInt(num) // extracts a numeric value from the
// start of the string, or NaN.
Run Code Online (Sandbox Code Playgroud)
parseInt('12') // 12
parseInt('aaa') // NaN
parseInt('12px') // 12
parseInt('foo2') // NaN These last two may be different
parseInt('12a5') // 12 from what you expected to see.
Run Code Online (Sandbox Code Playgroud)
记住,不像熊+num
,parseInt
(顾名思义)将一个float转换为整数通过斩去一切小数点后面(如果你想使用parseInt()
,因为这种行为,你可能会更好过使用其他方法代替) :
+'12.345' // 12.345
parseInt(12.345) // 12
parseInt('12.345') // 12
Run Code Online (Sandbox Code Playgroud)
空字符串可能有点违反直觉.+num
将空字符串转换为零,并isNaN()
假设相同:
+'' // 0
isNaN('') // false
Run Code Online (Sandbox Code Playgroud)
但parseInt()
不同意:
parseInt('') // NaN
Run Code Online (Sandbox Code Playgroud)
roe*_*ing 48
你可以去RegExp方式:
var num = "987238";
if(num.match(/^-{0,1}\d+$/)){
//valid integer (positive or negative)
}else if(num.match(/^\d+\.\d+$/)){
//valid float
}else{
//not valid number
}
Run Code Online (Sandbox Code Playgroud)
Gav*_*vin 40
如果你只是想检查字符串是否是一个整数(没有小数位),正则表达式是一个很好的方法.其他方法如isNaN
对于如此简单的事情来说太复杂了.
function isNumeric(value) {
return /^-{0,1}\d+$/.test(value);
}
console.log(isNumeric('abcd')); // false
console.log(isNumeric('123a')); // false
console.log(isNumeric('1')); // true
console.log(isNumeric('1234567890')); // true
console.log(isNumeric('-23')); // true
console.log(isNumeric(1234)); // true
console.log(isNumeric('123.4')); // false
console.log(isNumeric('')); // false
console.log(isNumeric(undefined)); // false
console.log(isNumeric(null)); // false
Run Code Online (Sandbox Code Playgroud)
要仅允许正整数使用此:
function isNumeric(value) {
return /^\d+$/.test(value);
}
console.log(isNumeric('123')); // true
console.log(isNumeric('-23')); // false
Run Code Online (Sandbox Code Playgroud)
Mic*_*ael 35
如果你真的想确保字符串只包含一个数字,任何数字(整数或浮点数),并准确一些,你不能使用parseInt()
/ parseFloat()
,Number()
或!isNaN()
自己.需要注意的是!isNaN()
真的返回true
时,Number()
将返回一个数字,false
当它会返回NaN
,所以我会从讨论的其余排除.
的问题parseFloat()
是,它会返回一个数字,如果字符串中包含任何数量,即使字符串不包含只和准确的数字:
parseFloat("2016-12-31") // returns 2016
parseFloat("1-1") // return 1
parseFloat("1.2.3") // returns 1.2
Run Code Online (Sandbox Code Playgroud)
问题Number()
是,如果传递的值根本不是数字,它将返回一个数字!
Number("") // returns 0
Number(" ") // returns 0
Number(" \u00A0 \t\n\r") // returns 0
Run Code Online (Sandbox Code Playgroud)
滚动自己的正则表达式的问题在于,除非您创建用于匹配浮点数的精确正则表达式,因为Javascript会识别它,否则您将错过案例或识别不应该的情况.即使你可以推出自己的正则表达式,为什么呢?有更简单的内置方法可以做到这一点.
然而,事实证明,Number()
(并且isNaN()
)对于每个parseFloat()
返回数字的情况都做对了,反之亦然.因此,要查明字符串是否确实只是一个数字,请调用这两个函数并查看它们是否都返回true:
function isNumber(str) {
if (typeof str != "string") return false // we only process strings!
// could also coerce to string: str = ""+str
return !isNaN(str) && !isNaN(parseFloat(str))
}
Run Code Online (Sandbox Code Playgroud)
Has*_*bel 35
这很大程度上取决于您想要解析为数字的内容。
\n由于现有的资源都不能满足我的需求,我试图弄清楚这些函数到底发生了什么。
\n这个问题的三个直接答案是:
\n!isNaN(input)
(其输出与 相同+input === +input
)!isNaN(parseFloat(input))
isFinite(input)
但它们中的任何一个在每种情况下都是正确的吗?
\n我在几种情况下测试了这些函数,并以降价形式生成输出。它看起来是这样的:
\ninput | !isNaN(input) 或者+input===+input | !isNaN( parseFloat( input)) | isFinite( input) | 评论 |
---|---|---|---|---|
123 | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | - |
\'123\' | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | - |
12.3 | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | - |
'12.3' | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | - |
\'\xc2\xa0\xc2\xa0 12.3\xc2\xa0\xc2\xa0 \' | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | 正如预期的那样,空白空格已被修剪。 |
1_000_000 | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | 数字分隔符已被理解,也是预期的。 |
\'1_000_000\' | \xe2\x9d\x8c | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9d\x8c | 惊喜!JS 只是不会解析字符串内的数字分隔符。有关详细信息,请检查此问题。(为什么解析为 float 有效?嗯,它没有。) |
\'0b11111111\' | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | 二进制形式应该被理解。 |
\'0o377\' | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | 八进制形式也可以理解。 |
\'0xFF\' | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | 当然可以理解十六进制。有人不这么认为吗? |
'' | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9d\x8c | \xe2\x9c\x94\xef\xb8\x8f | 空字符串应该是数字吗? |
\'\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\' | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9d\x8c | \xe2\x9c\x94\xef\xb8\x8f | 纯空白字符串应该是数字吗? |
\'abc\' | \xe2\x9d\x8c | \xe2\x9d\x8c | \xe2\x9d\x8c | 每个人都同意,而不是一个数字。 |
\'12.34Ab!@#$\' | \xe2\x9d\x8c | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9d\x8c | 啊! 现在已经可以理解它的parseFloat() 作用了。对我来说并不令人印象深刻,但在某些情况下可能会派上用场。 |
\'10e100\' | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | 10100确实是一个数字。 但要小心!它比最大安全整数值 2 53(大约 9\xc3\x9710 15 )大得多。阅读本文了解详细信息。 |
\'10e1000\' | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9d\x8c | 跟我说一声,救命啊! 尽管并不像看起来那么疯狂。在 JavaScript 中,大于 ~10 308的值会四舍五入为无穷大,这就是原因。查看此处了解详细信息。 是的, isNaN() 将无穷大视为一个数字,并将parseFloat() 无穷大解析为无穷大。 |
无效的 | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9d\x8c | \xe2\x9c\x94\xef\xb8\x8f | 现在这很尴尬。在 JS 中,当需要转换时,null 变成零,我们得到一个有限的数字。 那为什么 parseFloat(null) 要在这里返回一个NaN 呢?请有人向我解释一下这个设计理念。 |
不明确的 | \xe2\x9d\x8c | \xe2\x9d\x8c | \xe2\x9d\x8c | 正如预期的那样。 |
无穷 | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9c\x94\xef\xb8\x8f | \xe2\x9d\x8c | 如前所述,isNaN() 将无穷大视为数字,并将parseFloat() 无穷大解析为无穷大。 |
那么......其中哪一个是“正确的”?
\n现在应该很清楚了,这很大程度上取决于我们需要什么。例如,我们可能希望将空输入视为 0。在这种情况下isFinite()
就可以正常工作。
isNaN()
再说一次,也许我们会在需要将10 10000000000视为有效数字时得到一点帮助(尽管问题仍然是\xe2\x80\x94为什么会这样,以及我们将如何处理)!
当然,我们可以手动排除任何场景。
\n就像我的例子一样,我完全需要 的输出isFinite()
,除了 null 情况、空字符串情况和仅空白字符串情况。而且我对巨大的数字并不感到头疼。所以我的代码看起来像这样:
/**\n * My necessity was met by the following code.\n */\n\nif (input === null) {\n // Null input\n} else if (input.trim() === \'\') {\n // Empty or whitespace-only string\n} else if (isFinite(input)) {\n // Input is a number\n} else {\n // Not a number\n}\n
Run Code Online (Sandbox Code Playgroud)\n而且,这是我生成表格的 JavaScript:
\n/**\n * Note: JavaScript does not print numeric separator inside a number.\n * In that single case, the markdown output was manually corrected.\n * Also, the comments were manually added later, of course.\n */\n\nlet inputs = [\n 123, \'123\', 12.3, \'12.3\', \' 12.3 \',\n 1_000_000, \'1_000_000\',\n \'0b11111111\', \'0o377\', \'0xFF\',\n \'\', \' \',\n \'abc\', \'12.34Ab!@#$\',\n \'10e100\', \'10e1000\',\n null, undefined, Infinity];\n\nlet markdownOutput = `| \\`input\\` | \\`!isNaN(input)\\` or <br>\\`+input === +input\\` | \\`!isNaN(parseFloat(input))\\` | \\`isFinite(input)\\` | Comment |\n| :---: | :---: | :---: | :---: | :--- |\\n`;\n\nfor (let input of inputs) {\n let outputs = [];\n outputs.push(!isNaN(input));\n outputs.push(!isNaN(parseFloat(input)));\n outputs.push(isFinite(input));\n\n if (typeof input === \'string\') {\n // Output with quotations\n console.log(`\'${input}\'`);\n markdownOutput += `| \'${input}\'`;\n } else {\n // Output without quotes\n console.log(input);\n markdownOutput += `| ${input}`;\n }\n\n for (let output of outputs) {\n console.log(\'\\t\' + output);\n if (output === true) {\n markdownOutput += ` | <div style="color:limegreen">true</div>`;\n // markdownOutput += ` | \xe2\x9c\x94\xef\xb8\x8f`; // for stackoverflow\n } else {\n markdownOutput += ` | <div style="color:orangered">false</div>`;\n // markdownOutput += ` | \xe2\x9d\x8c`; // for stackoverflow\n }\n }\n\n markdownOutput += ` ||\\n`;\n}\n\n// Replace two or more whitespaces with $nbsp;\nmarkdownOutput = markdownOutput.replaceAll(` `, ` `);\n\n// Print markdown to console\nconsole.log(markdownOutput);\n
Run Code Online (Sandbox Code Playgroud)\n
小智 28
JavaScript 全局isFinite()
检查值是否是有效(有限)数字。
请参阅 MDN 了解Number.isFinite() 和全局 isFinite() 之间的区别。
let a = isFinite('abc') // false
let b = isFinite('123') // true
let c = isFinite('12a') // false
let d = isFinite(null) // true
console.log(a, b, c, d)
Run Code Online (Sandbox Code Playgroud)
the*_*ear 21
尝试isNan函数:
isNaN()函数确定值是否为非法数字(非数字).
如果值等于NaN,则此函数返回true.否则返回false.
此函数与Number specific Number.isNaN()方法不同.
全局isNaN()函数,将测试值转换为Number,然后对其进行测试.
Number.isNan()不会将值转换为Number,并且对于任何不是Number类型的值都不会返回true.
chi*_*ens 18
有人也可能从基于正则表达式的答案中受益。这里是:
其中一个衬垫是整数:
const isInteger = num => /^-?[0-9]+$/.test(num+'');
Run Code Online (Sandbox Code Playgroud)
一行 isNumeric:接受整数和小数
const isNumeric = num => /^-?[0-9]+(?:\.[0-9]+)?$/.test(num+'');
Run Code Online (Sandbox Code Playgroud)
Ham*_*eem 17
这个问题的公认答案有很多缺陷(正如其他几个用户所强调的那样).这是在javascript中处理它的最简单且经过验证的方法之一:
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
Run Code Online (Sandbox Code Playgroud)
以下是一些很好的测试用例:
console.log(isNumeric(12345678912345678912)); // true
console.log(isNumeric('2 ')); // true
console.log(isNumeric('-32.2 ')); // true
console.log(isNumeric(-32.2)); // true
console.log(isNumeric(undefined)); // false
// the accepted answer fails at these tests:
console.log(isNumeric('')); // false
console.log(isNumeric(null)); // false
console.log(isNumeric([])); // false
Run Code Online (Sandbox Code Playgroud)
mar*_*ark 11
老问题,但在给定的答案中有几点缺失.
科学计数法.
!isNaN('1e+30')
是true
,但在大多数情况下,当人们问数字,他们不想匹配类的东西1e+30
.
大浮点数可能表现得很奇怪
观察(使用Node.js):
> var s = Array(16 + 1).join('9')
undefined
> s.length
16
> s
'9999999999999999'
> !isNaN(s)
true
> Number(s)
10000000000000000
> String(Number(s)) === s
false
>
Run Code Online (Sandbox Code Playgroud)
另一方面:
> var s = Array(16 + 1).join('1')
undefined
> String(Number(s)) === s
true
> var s = Array(15 + 1).join('9')
undefined
> String(Number(s)) === s
true
>
Run Code Online (Sandbox Code Playgroud)
因此,如果有人期望String(Number(s)) === s
,那么最好将字符串最多限制为15位(省略前导零后).
无穷
> typeof Infinity
'number'
> !isNaN('Infinity')
true
> isFinite('Infinity')
false
>
Run Code Online (Sandbox Code Playgroud)
鉴于此,检查给定字符串是否满足以下所有条件:
Number
回到String
这不是一件容易的事.这是一个简单的版本:
function isNonScientificNumberString(o) {
if (!o || typeof o !== 'string') {
// Should not be given anything but strings.
return false;
}
return o.length <= 15 && o.indexOf('e+') < 0 && o.indexOf('E+') < 0 && !isNaN(o) && isFinite(o);
}
Run Code Online (Sandbox Code Playgroud)
然而,即便是这个还远未完成.这里没有处理前导零,但它们确实拧长度测试.
Jer*_*emy 10
也许这已经被重述了太多次,但是我今天也与这个进行了斗争并想发布我的答案,因为我没有看到任何其他答案可以简单或彻底地做到这一点:
var isNumeric = function(num){
return (typeof(num) === 'number' || typeof(num) === "string" && num.trim() !== '') && !isNaN(num);
}
Run Code Online (Sandbox Code Playgroud)
const isNumeric = (num) => (typeof(num) === 'number' || typeof(num) === "string" && num.trim() !== '') && !isNaN(num);
Run Code Online (Sandbox Code Playgroud)
const isNumeric = (num: any) => (typeof(num) === 'number' || typeof(num) === "string" && num.trim() !== '') && !isNaN(num as number);
Run Code Online (Sandbox Code Playgroud)
这看起来很简单,涵盖了我在许多其他帖子中看到并自己想到的所有基础:
// Positive Cases
console.log(0, isNumeric(0) === true);
console.log(1, isNumeric(1) === true);
console.log(1234567890, isNumeric(1234567890) === true);
console.log('1234567890', isNumeric('1234567890') === true);
console.log('0', isNumeric('0') === true);
console.log('1', isNumeric('1') === true);
console.log('1.1', isNumeric('1.1') === true);
console.log('-1', isNumeric('-1') === true);
console.log('-1.2354', isNumeric('-1.2354') === true);
console.log('-1234567890', isNumeric('-1234567890') === true);
console.log(-1, isNumeric(-1) === true);
console.log(-32.1, isNumeric(-32.1) === true);
console.log('0x1', isNumeric('0x1') === true); // Valid number in hex
// Negative Cases
console.log(true, isNumeric(true) === false);
console.log(false, isNumeric(false) === false);
console.log('1..1', isNumeric('1..1') === false);
console.log('1,1', isNumeric('1,1') === false);
console.log('-32.1.12', isNumeric('-32.1.12') === false);
console.log('[blank]', isNumeric('') === false);
console.log('[spaces]', isNumeric(' ') === false);
console.log('null', isNumeric(null) === false);
console.log('undefined', isNumeric(undefined) === false);
console.log([], isNumeric([]) === false);
console.log('NaN', isNumeric(NaN) === false);
Run Code Online (Sandbox Code Playgroud)
您也可以尝试自己的isNumeric
函数,然后在这些用例中过去,然后为所有用例扫描“true”。
或者,查看每个返回的值:
我已经测试过,迈克尔的解决方案是最好的.投票给他上面的答案(搜索此页面"如果你真的想确保一个字符串"找到它).从本质上讲,他的回答如下:
function isNumeric(num){
num = "" + num; //coerce num to be a string
return !isNaN(num) && !isNaN(parseFloat(num));
}
Run Code Online (Sandbox Code Playgroud)
它适用于我在此处记录的每个测试用例:https: //jsfiddle.net/wggehvp9/5/
许多其他解决方案因这些边缘情况而失败:'',null,"",true和[].从理论上讲,您可以使用它们,并进行适当的错误处理,例如:
return !isNaN(num);
Run Code Online (Sandbox Code Playgroud)
要么
return (+num === +num);
Run Code Online (Sandbox Code Playgroud)
特殊处理/\s /,null,"",true,false,[](以及其他?)
我喜欢这种简单性。
Number.isNaN(Number(value))
Run Code Online (Sandbox Code Playgroud)
上面是常规的 Javascript,但我将其与打字稿类型保护程序结合使用以进行智能类型检查。这对于打字稿编译器非常有用,可以为您提供正确的智能感知,并且没有类型错误。
警告:请参阅下面杰里米的评论。这对某些值有一些问题,我现在没有时间修复它,但是使用 typescript typeguard 的想法很有用,所以我不会删除此部分。
isNotNumber(value: string | number): value is string {
return Number.isNaN(Number(this.smartImageWidth));
}
isNumber(value: string | number): value is number {
return Number.isNaN(Number(this.smartImageWidth)) === false;
}
Run Code Online (Sandbox Code Playgroud)
假设您有一个属性width
是number | string
。您可能想要根据它是否是字符串来执行逻辑。
var width: number|string;
width = "100vw";
if (isNotNumber(width))
{
// the compiler knows that width here must be a string
if (width.endsWith('vw'))
{
// we have a 'width' such as 100vw
}
}
else
{
// the compiler is smart and knows width here must be number
var doubleWidth = width * 2;
}
Run Code Online (Sandbox Code Playgroud)
width
类型保护器足够聪明,可以将语句中的类型限制if
为 ONLY string
。这允许编译器允许width.endsWith(...)
如果类型为 则不允许的情况string | number
。
你可以随意称呼 typeguard isNotNumber
, isNumber
, isString
,isNotString
但我认为isString
这有点含糊并且难以阅读。
将参数传递给构造函数时,可以使用Number的结果。
如果参数(字符串)不能转换为数字,则返回NaN,因此您可以确定所提供的字符串是否为有效数字。
注意:当传递空字符串或 '\t\t'
and '\n\t'
作为Number时,将返回0;传递true将返回1,而false则返回0。
Number('34.00') // 34
Number('-34') // -34
Number('123e5') // 12300000
Number('123e-5') // 0.00123
Number('999999999999') // 999999999999
Number('9999999999999999') // 10000000000000000 (integer accuracy up to 15 digit)
Number('0xFF') // 255
Number('Infinity') // Infinity
Number('34px') // NaN
Number('xyz') // NaN
Number('true') // NaN
Number('false') // NaN
// cavets
Number(' ') // 0
Number('\t\t') // 0
Number('\n\t') // 0
Run Code Online (Sandbox Code Playgroud)
为什么 jQuery 的实现不够好?
function isNumeric(a) {
var b = a && a.toString();
return !$.isArray(a) && b - parseFloat(b) + 1 >= 0;
};
Run Code Online (Sandbox Code Playgroud)
迈克尔提出了这样的建议(虽然我在这里偷了“user1691651 - John”的修改版本):
function isNumeric(num){
num = "" + num; //coerce num to be a string
return !isNaN(num) && !isNaN(parseFloat(num));
}
Run Code Online (Sandbox Code Playgroud)
以下是一个很可能性能不佳但结果可靠的解决方案。这是一个由 jQuery 1.12.4 实现和 Michael 的答案制成的装置,额外检查前导/尾随空格(因为 Michael 的版本对于带有前导/尾随空格的数字返回 true):
function isNumeric(a) {
var str = a + "";
var b = a && a.toString();
return !$.isArray(a) && b - parseFloat(b) + 1 >= 0 &&
!/^\s+|\s+$/g.test(str) &&
!isNaN(str) && !isNaN(parseFloat(str));
};
Run Code Online (Sandbox Code Playgroud)
不过,后一个版本有两个新变量。可以通过执行以下操作来解决其中之一:
function isNumeric(a) {
if ($.isArray(a)) return false;
var b = a && a.toString();
a = a + "";
return b - parseFloat(b) + 1 >= 0 &&
!/^\s+|\s+$/g.test(a) &&
!isNaN(a) && !isNaN(parseFloat(a));
};
Run Code Online (Sandbox Code Playgroud)
除了手动测试我将遇到的少数用例之外,我还没有对其中的任何一个进行过太多测试,这些用例都是非常标准的东西。这是一种“站在巨人的肩膀上”的情况。
引用:
isNaN(num) // 如果变量不包含有效数字,则返回 true
如果您需要检查前导/尾随空格,则不完全正确 - 例如,当需要一定数量的数字时,您需要获取“1111”而不是“111”或“111”作为可能的 PIN输入。
更好地使用:
var num = /^\d+$/.test(num)
Run Code Online (Sandbox Code Playgroud)
小智 6
也许有一两个人遇到这个问题需要比平常更严格的检查(就像我做的那样).在这种情况下,这可能是有用的:
if(str === String(Number(str))) {
// it's a "perfectly formatted" number
}
Run Code Online (Sandbox Code Playgroud)
谨防!这将拒绝像琴弦.1
,40.000
,080
,00.1
.这是非常挑剔的 - 字符串必须匹配此测试通过的数字的" 最小完美形式 ".
它使用String
和Number
构造函数将字符串转换为数字并再次返回,从而检查JavaScript引擎的"完美最小形式"(它与初始Number
构造函数转换为的形式)是否与原始字符串匹配.
当防止空字符串和null
// Base cases that are handled properly
Number.isNaN(Number('1')); // => false
Number.isNaN(Number('-1')); // => false
Number.isNaN(Number('1.1')); // => false
Number.isNaN(Number('-1.1')); // => false
Number.isNaN(Number('asdf')); // => true
Number.isNaN(Number(undefined)); // => true
// Special notation cases that are handled properly
Number.isNaN(Number('1e1')); // => false
Number.isNaN(Number('1e-1')); // => false
Number.isNaN(Number('-1e1')); // => false
Number.isNaN(Number('-1e-1')); // => false
Number.isNaN(Number('0b1')); // => false
Number.isNaN(Number('0o1')); // => false
Number.isNaN(Number('0xa')); // => false
// Edge cases that will FAIL if not guarded against
Number.isNaN(Number('')); // => false
Number.isNaN(Number(' ')); // => false
Number.isNaN(Number(null)); // => false
// Edge cases that are debatable
Number.isNaN(Number('-0b1')); // => true
Number.isNaN(Number('-0o1')); // => true
Number.isNaN(Number('-0xa')); // => true
Number.isNaN(Number('Infinity')); // => false
Number.isNaN(Number('INFINITY')); // => true
Number.isNaN(Number('-Infinity')); // => false
Number.isNaN(Number('-INFINITY')); // => true
Run Code Online (Sandbox Code Playgroud)
当不防范空字符串时并且null
使用parseInt
:
// Base cases that are handled properly
Number.isNaN(parseInt('1')); // => false
Number.isNaN(parseInt('-1')); // => false
Number.isNaN(parseInt('1.1')); // => false
Number.isNaN(parseInt('-1.1')); // => false
Number.isNaN(parseInt('asdf')); // => true
Number.isNaN(parseInt(undefined)); // => true
Number.isNaN(parseInt('')); // => true
Number.isNaN(parseInt(' ')); // => true
Number.isNaN(parseInt(null)); // => true
// Special notation cases that are handled properly
Number.isNaN(parseInt('1e1')); // => false
Number.isNaN(parseInt('1e-1')); // => false
Number.isNaN(parseInt('-1e1')); // => false
Number.isNaN(parseInt('-1e-1')); // => false
Number.isNaN(parseInt('0b1')); // => false
Number.isNaN(parseInt('0o1')); // => false
Number.isNaN(parseInt('0xa')); // => false
// Edge cases that are debatable
Number.isNaN(parseInt('-0b1')); // => false
Number.isNaN(parseInt('-0o1')); // => false
Number.isNaN(parseInt('-0xa')); // => false
Number.isNaN(parseInt('Infinity')); // => true
Number.isNaN(parseInt('INFINITY')); // => true
Number.isNaN(parseInt('-Infinity')); // => true
Number.isNaN(parseInt('-INFINITY')); // => true
Run Code Online (Sandbox Code Playgroud)
使用parseFloat
:
// Base cases that are handled properly
Number.isNaN(parseFloat('1')); // => false
Number.isNaN(parseFloat('-1')); // => false
Number.isNaN(parseFloat('1.1')); // => false
Number.isNaN(parseFloat('-1.1')); // => false
Number.isNaN(parseFloat('asdf')); // => true
Number.isNaN(parseFloat(undefined)); // => true
Number.isNaN(parseFloat('')); // => true
Number.isNaN(parseFloat(' ')); // => true
Number.isNaN(parseFloat(null)); // => true
// Special notation cases that are handled properly
Number.isNaN(parseFloat('1e1')); // => false
Number.isNaN(parseFloat('1e-1')); // => false
Number.isNaN(parseFloat('-1e1')); // => false
Number.isNaN(parseFloat('-1e-1')); // => false
Number.isNaN(parseFloat('0b1')); // => false
Number.isNaN(parseFloat('0o1')); // => false
Number.isNaN(parseFloat('0xa')); // => false
// Edge cases that are debatable
Number.isNaN(parseFloat('-0b1')); // => false
Number.isNaN(parseFloat('-0o1')); // => false
Number.isNaN(parseFloat('-0xa')); // => false
Number.isNaN(parseFloat('Infinity')); // => false
Number.isNaN(parseFloat('INFINITY')); // => true
Number.isNaN(parseFloat('-Infinity')); // => false
Number.isNaN(parseFloat('-INFINITY')); // => true
Run Code Online (Sandbox Code Playgroud)
笔记:
Infinity
(区分大小写)不同,以字符串格式作为测试用例传递给上述任何方法的 和 对象中的常量将被确定为不是数字Number
。Math
Number
null
通常,“有效数字”是指不包括 NaN 和无穷大的 Javascript 数字,即“有限数字”。
要检查值的数值有效性(例如来自外部来源),您可以在 ESlint Airbnb 样式中定义:
/**
* Returns true if 'candidate' is a finite number or a string referring (not just 'including') a finite number
* To keep in mind:
* Number(true) = 1
* Number('') = 0
* Number(" 10 ") = 10
* !isNaN(true) = true
* parseFloat('10 a') = 10
*
* @param {?} candidate
* @return {boolean}
*/
function isReferringFiniteNumber(candidate) {
if (typeof (candidate) === 'number') return Number.isFinite(candidate);
if (typeof (candidate) === 'string') {
return (candidate.trim() !== '') && Number.isFinite(Number(candidate));
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
并以这种方式使用它:
if (isReferringFiniteNumber(theirValue)) {
myCheckedValue = Number(theirValue);
} else {
console.warn('The provided value doesn\'t refer to a finite number');
}
Run Code Online (Sandbox Code Playgroud)
这样对我有用。
function isNumeric(num){
let value1 = num.toString();
let value2 = parseFloat(num).toString();
return (value1 === value2);
}
Run Code Online (Sandbox Code Playgroud)
console.log(
isNumeric(123), //true
isNumeric(-123), //true
isNumeric('123'), //true
isNumeric('-123'), //true
isNumeric(12.2), //true
isNumeric(-12.2), //true
isNumeric('12.2'), //true
isNumeric('-12.2'), //true
isNumeric('a123'), //false
isNumeric('123a'), //false
isNumeric(' 123'), //false
isNumeric('123 '), //false
isNumeric('a12.2'), //false
isNumeric('12.2a'), //false
isNumeric(' 12.2'), //false
isNumeric('12.2 '), //false
)
Run Code Online (Sandbox Code Playgroud)
parseInt(),但要注意这个函数有点不同,例如它为parseInt("100px")返回100.
它对 TypeScript 无效,因为:
declare function isNaN(number: number): boolean;
对于打字稿,您可以使用:
/^\d+$/.test(key)
归档时间: |
|
查看次数: |
615108 次 |
最近记录: |