parseInt always returns NaN?

后端 未结 4 625
太阳男子
太阳男子 2020-12-06 12:48

long story short, i was trying to validate a phone field. ive added the isNaN and parseInt for checking the \" \" in the field but tha

4条回答
  •  没有蜡笔的小新
    2020-12-06 13:18

    I've seen Number() suggested, but that will still allow things like -21 or 123.456. The best way to check for the absence of non-digits in a string is like this:

    function hasNonDigit(str){
      return /\D/g.test(str.toString());
    }
    
    console.log(hasNonDigit("123-456-7890"));
    console.log(hasNonDigit("1234567890"));

提交回复
热议问题