JS里怎么使用isdigit方法

js文件里的函数:
function value_phone(phone_num){
if(phone_num.isdigit() && phone_num.length == 11 && phone_num.startsWith('1')){
document.getElementById("phone").innerHTML = '手机号码格式正确';
}else{
document.getElementById("phone").innerHTML = '手机号码格式不正确';
}
}

报错:
UncaughtTypeError: phone_num.isdigit is not a function

img

你如果要校验手机号,用正则匹配好一些。

if(/^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/.test(phone_num)){
  console.log("手机号正确");
}else{
  console.log("手机号不正确");
}

原生js没有内置isdigit方法,可以写一个


function isDigit(value) {
    var patrn = /^[0-9]*$/;
    if (patrn.exec(value) == null || value == "") {
        return false
    } else {
        return true
    }
}
console.log(isDigit(1)) // true

https://www.cnblogs.com/shaoing/p/5358138.html