An improved isNumeric() function?

前端 未结 7 1604
轮回少年
轮回少年 2021-02-01 14:00

During some projects I have needed to validate some data and be as certain as possible that it is javascript numerical value that can be used in mathematical operations.

7条回答
  •  心在旅途
    2021-02-01 14:57

    function isNumber(value){
        return !isNaN(parseFloat(value)) && 
            isFinite(value.toString().replace(/^-/, '')) && 
            typeof value !== 'object';
    

    }

    or :

    function isNumber(value){
        return !Array.isArray(value) && !isNaN(parseFloat(value)) && 
            isFinite(value.toString().replace(/^-/, '')) && 
            Object.prototype.toString.call(value) !== '[object Object]';
    }
    

提交回复
热议问题