Skip to content

表单校验

  • 手机号
/**
  * @param {string || Number} phoneNum - 手机号.
*/
function checkPhoneNum(phoneNum){
    let checkRule =/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    if(!checkRule.test(phoneNum)){
        return false
    }else{
        return true
    }
}
  • 电子邮箱
/**
  * @param {string} postbox - 邮箱号.
*/
function checkPostbox(postbox){
    let checkRule =/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
    if(!checkRule.test(postbox)){
        return false
    }else{
        return true
    }
}
  • 身份证
/**
  * @param {string || Number} idNum - 身份证号.
*/
function checkIdNum(idNum){
    let checkRule =/^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
    if(!checkRule.test(idNum)){
        return false
    }else{
        return true
    }
}
  • 身份证后六位
/**
  * @param {string || Number} idLastSixNum - 身份证后六位.
*/
function checkIdLastSixNum(idLastSixNum){
    let checkRule =/^(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
    if(!checkRule.test(idLastSixNum)){
        return false
    }else{
        return true
    }
}
  • QQ号
/**
  * @param {string || Number} qqNum - QQ号.
*/
function checkQqNum(qqNum){
    let checkRule =/^[1-9][0-9]\d{4,9}$/
    if(!checkRule.test(qqNum)){
        return false
    }else{
        return true
    }
}
  • 邮政编码
/**
  * @param {string || Number} postalCode - 邮政编码.
*/
function checkPostalCode(postalCode){
    let checkRule =/^[1-9]\d{5}$/
    if(!checkRule.test(postalCode)){
        return false
    }else{
        return true
    }
}