js校验手机号、邮政编码、email、url的数据合法性

复制代码
//校验手机号/固话
function checkPhone(mobile) {
    mobile = mobile + '';
    var tel = /^0\d{2,3}-?\d{7,8}$/;
    var phone = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

    if (mobile.length === 11) {//手机号码
        if (phone.test(mobile)) {
            return true;
        }
    } else if (mobile.length === 12 && mobile.indexOf("-") !== -1) {
        if (tel.test(mobile)) {
            return true;
        }
    }
    return false;
}

//校验邮政编码
function checkPostcode(postcode) {
    if ( postcode === "") {
        return false;
    } else {
        if (! /^[0-9][0-9]{5}$/.test(postcode)) {
            return false;
        }
    }
    return true;
}

//校验email的合法性
function checkEmail(email) {
    if (email === "") {
        return false;
    } else {
        if (!/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/.test(email)) {
            return false;
        }
    }
    return true;
}

//验证url的格式合法性
function isUrl(str_url) {
    let strRegex = "^((https|http|ftp|rtsp|mms)?://)"
        + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" // ftp的user@
        + "(([0-9]{1,3}\.){3}[0-9]{1,3}"                             // IP形式的URL- 199.194.52.184
        + "|"                                                        // 允许IP和DOMAIN(域名)
        + "([0-9a-z_!~*'()-]+\.)*"                                   // 域名- www.
        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\."                     // 二级域名
        + "[a-z]{2,6})"                                              // first level domain- .com or .museum
        + "(:[0-9]{1,4})?"                                           // 端口- :80
        + "((/?)|"                                                   // a slash isn't required if there is no file name
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
    let re = new RegExp(strRegex);
    return re.test(str_url);
}
复制代码

原文:https://blog.csdn.net/bfboys/article/details/52863846

        https://blog.csdn.net/UD_World/article/details/120462425

        https://www.xp.cn/b.php/100124.html

posted @   DAYTOY-105  阅读(214)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示