我希望在与旧的VB6 IsNumeric()函数相同的概念空间中有什么东西?
当前回答
2019:包括ES3、ES6和TypeScript示例
也许这已经被重复了太多次了,但是我今天也和这一个进行了斗争,并想发布我的答案,因为我没有看到任何其他答案能如此简单或彻底地做到这一点:
ES3
var isNumeric = function(num){
return (typeof(num) === 'number' || typeof(num) === "string" && num.trim() !== '') && !isNaN(num);
}
ES6
const isNumeric = (num) => (typeof(num) === 'number' || typeof(num) === "string" && num.trim() !== '') && !isNaN(num);
字体
const isNumeric = (num: any) => (typeof(num) === 'number' || typeof(num) === "string" && num.trim() !== '') && !isNaN(num as number);
这似乎很简单,涵盖了我在许多其他帖子中看到的所有基础,并自己思考:
// 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);
您还可以尝试自己的isNumeric函数,并在这些用例中刚刚过去,然后扫描所有用例的“true”。
或者,查看每个返回的值:
其他回答
因此,这将取决于您希望它处理的测试用例。
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
截至今天,所有其他答案似乎都未能通过其中一个测试用例。
我喜欢这种简单。
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有点模棱两可,很难理解。
测试字符串或数字是否为数字
const isNumeric = stringOrNumber =>
stringOrNumber == 0 || !!+stringOrNumber;
或者如果要将字符串或数字转换为数字
const toNumber = stringOrNumber =>
stringOrNumber == 0 || +stringOrNumber ? +stringOrNumber : NaN;
PFB工作溶液:
function(check){
check = check + "";
var isNumber = check.trim().length>0? !isNaN(check):false;
return isNumber;
}
报价:
isNaN(num)//如果变量不包含有效数字,则返回true
如果您需要检查前导/尾随空格,则不完全正确-例如,当需要一定数量的数字时,并且您需要获得例如“1111”而不是“111”或“111”以获得PIN输入。
更好使用:
var num = /^\d+$/.test(num)
推荐文章
- 转换php数组到Javascript
- 如何跳转到浏览器页面的顶部
- 在电子邮件消息中是否支持JavaScript ?
- 如何在JavaScript中指定Math.log()的基?
- 多重左赋值JavaScript
- 如何在单个测试基础上更改模拟实现?
- VueJS有条件地为元素添加属性
- Uncaught TypeError:(中间值)(…)不是一个函数
- 如何设置URL查询参数在Vue与Vue路由器
- 无法读取属性“addEventListener”为空
- 如何防止moment.js从webpack加载地区?
- getMonth在javascript中给出前一个月
- 如何在禁用按钮上启用引导工具提示?
- Node.js全局变量
- 在前一个函数完成后调用另一个函数