Regular verification of ID and url URL

 
// 身份证正则验证:
// 最后一位通过计算得到
// 且对长度进行验证。必须是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);
After the test, there is a problem with the verification above: input 111 can also pass the verification

Modified to: (must enter the address of http or https protocol)

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

Guess you like

Origin blog.csdn.net/qq_42269433/article/details/95635091
URL
URL
URL
URL