(Built-in) way in JavaScript to check if a string is a valid number

前端 未结 30 3841
-上瘾入骨i
-上瘾入骨i 2020-11-22 01:54

I\'m hoping there\'s something in the same conceptual space as the old VB6 IsNumeric() function?

30条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 02:40

    And you could go the RegExp-way:

    var num = "987238";
    
    if(num.match(/^-?\d+$/)){
      //valid integer (positive or negative)
    }else if(num.match(/^\d+\.\d+$/)){
      //valid float
    }else{
      //not valid number
    }
    

提交回复
热议问题