An improved isNumeric() function?

前端 未结 7 1576
轮回少年
轮回少年 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]';
    }
    
    0 讨论(0)
提交回复
热议问题