我希望在与旧的VB6 IsNumeric()函数相同的概念空间中有什么东西?
当前回答
这是基于之前的一些回答和评论。以下内容涵盖了所有边缘情况,也相当简洁:
const isNumRegEx = /^-?(\d*\.)?\d+$/;
function isNumeric(n, allowScientificNotation = false) {
return allowScientificNotation ?
!Number.isNaN(parseFloat(n)) && Number.isFinite(n) :
isNumRegEx.test(n);
}
其他回答
报价:
isNaN(num)//如果变量不包含有效数字,则返回true
如果您需要检查前导/尾随空格,则不完全正确-例如,当需要一定数量的数字时,并且您需要获得例如“1111”而不是“111”或“111”以获得PIN输入。
更好使用:
var num = /^\d+$/.test(num)
我的尝试有点混乱,Pherhaps不是最好的解决方案
function isInt(a){
return a === ""+~~a
}
console.log(isInt('abcd')); // false
console.log(isInt('123a')); // false
console.log(isInt('1')); // true
console.log(isInt('0')); // true
console.log(isInt('-0')); // false
console.log(isInt('01')); // false
console.log(isInt('10')); // true
console.log(isInt('-1234567890')); // true
console.log(isInt(1234)); // false
console.log(isInt('123.4')); // false
console.log(isInt('')); // false
// other types then string returns false
console.log(isInt(5)); // false
console.log(isInt(undefined)); // false
console.log(isInt(null)); // false
console.log(isInt('0x1')); // false
console.log(isInt(Infinity)); // false
因此,这将取决于您希望它处理的测试用例。
function isNumeric(number) {
return !isNaN(parseFloat(number)) && !isNaN(+number);
}
我要找的是javascript中的常规数字类型。0,1,-1,1.1,-1.1,1E1,-1E1,1E1,-1E1,0.1e10,-0.1e10,0xAF1,0o172,数学PI,数字.NEGATIVE_INFINITY,数字.POSITIVE_INFINIT
它们也是字符串表示:“0”、“1”、“-1”、“1.1”、“-1.1”、”1E1“、”-1E1“、”1E1“、”-1 e1“、“0.1e10”、”-0.1.e10“、”0xAF1“、“0o172”
我确实想省略,不要将它们标记为数字“”,“”,[],{},null,未定义,NaN
截至今天,所有其他答案似乎都未能通过其中一个测试用例。
这个问题的公认答案有很多缺陷(正如其他几位用户所强调的)。这是用javascript实现它的最简单且经过验证的方法之一:
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
以下是一些好的测试用例:
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
我喜欢这种简单。
Number.isNaN(Number(value))
以上是常规的Javascript,但我将其与typescript类型保护器一起用于智能类型检查。这对于类型脚本编译器提供正确的智能感知和无类型错误非常有用。
打字保护装置
警告:请参阅Jeremy在下面的评论。这在某些值上有一些问题,我现在没有时间修复它,但使用typescript类型保护的想法很有用,因此我不会删除此部分。
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;
}
假设您有一个属性宽度,即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;
}
类型保护足够聪明,可以将if语句中的宽度类型限制为ONLY字符串。这允许编译器允许width.endsWith(…),如果类型为string | number,则不允许。
你可以随意调用类型保护器isNotNumber,isNumber,isString,isNotString,但我认为isString有点模棱两可,很难理解。
推荐文章
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?
- 如何检查动态附加的事件监听器是否存在?
- 如何写setTimeout与参数Coffeescript
- 将JavaScript字符串中的多个空格替换为单个空格
- JavaScript: override alert()
- 重置setTimeout
- 如何确保<select>表单字段被禁用时提交?
- jQuery有不聚焦的方法吗?
- 反应钩子-正确的方式清除超时和间隔