正则验证身份证和url网址

博客提到url需进行修改,必须输入为http或者https协议的地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
// 身份证正则验证:
// 最后一位通过计算得到
// 且对长度进行验证。必须是18位合法的身份证。

const idExc =/^[1-9][0-7]\d{4}(((19|20)\d{2}(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((19|20)\d{2}(0[13578]|1[02])31)|((19|20)\d{2}02(0[1-9]|1\d|2[0-8]))|((19|20)([13579][26]|[2468][048]|0[48])0229))\d{3}(\d|X|x)?$/;
let pd = 0;
if (this.inputValue.length === 18) {
    const code = this.inputValue.split('');
    const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    const parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
    let sum = 0;
    let ai = 0;
    let wi = 0;
    for (let i = 0; i < 17; i++) {
       ai = code[i];
       wi = factor[i];
       sum += ai * wi;
    }
    const last = parity[sum % 11];
    if (last !== code[17]) {
        if (Number(last) !== Number(code[17])) {
            pd++;
        }
    }
}
if (!idExc.test(this.inputValue)) {
    pd++;
}
if (pd !== 0 || this.inputValue.length < 18) {
    this.showTips = true;
    this.tipMsg = '请输入正确的身份证号码';
}

url:

const sRegex = '^((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-zA-Z_!~*\'().;?:@&=+$,%#-]+)+/?)$';
const re = new RegExp(sRegex);
经过测试以上的验证有问题:输入111也能通过验证

修改为:(必须输入为http或者https协议的地址)

const sRegex = '^((ht|f)tps?):\\/\\/[\\w\\-]+(\\.[\\w\\-]+)+([\\w\\-.,@?^=%&:\\/~+#]*[\\w\\-@?^=%&\\/~+#])?$';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值