Js verification of regular phone number, QQ, ID cards, etc.

// common regular expressions check 
// QQ number, phone number, Email, whether it is digital, remove the spaces before and after, if there is Chinese, zip code, ID, URL, date format, IP 
the let myRegExp = {
     // check character QQ number string is a legal 
    isQQ: function (STR) {
         // . 1 ^ 0 can not be the first [1-9] 
        @ 2 must be [5, 11] digits \ D {. 4,. 9} 
        the let REG = / ^ [1-9] [0-9] {4,9} $ / GIM;
         IF (reg.test (STR)) { 
            the console.log ( 'QQ number to enter the correct format' );
             return  to true ; 
        } the else { 
            Console .log ( 'enter the number of the correct format, QQ' );
             return  to false ;
        } 
    } 
    // check whether the string is a legitimate phone number 
    isPhone: function (str) { 
        the let REG ? = / ^ (0 | 86 | 17951) (13 [0-9] | 15 [012 356 789] | 18 [0- . 9] | 14 [57 is] |. 17 [678]) [0-9]. 8} {$ / ;
         IF (reg.test (STR)) { 
            the console.log ( 'enter the correct phone number format' );
             return  to true ; 
        } the else { 
            the console.log ( 'enter the phone number of the correct format' );
             return  to false ; 
        } 
    }, 
    // check whether the string is valid Email address 
    isEmail: function (STR) {
        REG the let= /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+) . * \ [A-Za-Z0-9] + $ / ;
         IF (reg.test (STR)) { 
            the console.log ( 'In Email correct input format' );
             return  to true ; 
        } the else { 
            the console.log ( 'please input the correct format in Email ' );
             return  to false ; 
        } 
    }, 
    // check whether the string number 
    isNumber: function (STR) { 
        the let REG = / ^ \ + $ D / ;
         iF (reg.test (STR)) { 
            Console .log (STR + 'digital');
             Return  to true ; 
        } the else { 
            the console.log (STR + 'not a number' );
             return  to false ; 
        } 
    }, 
    // spaces before removing 
    TRIM: function (STR) { 
        the let REG = / ^ \ S + | \ S + $ / G;
         return str.replace (REG, '' ); 
    }, 
    // check whether there is a string Chinese 
    isChinese: function (STR) { 
        the let REG = / [\ u4e00- \ u9fa5] / GM;
         iF  (reg.test (str)) {
            the console.log (STR 'presence in Chinese' + );
             return  to true ; 
        } the else { 
            the console.log (STR + 'does not exist in Chinese' );
             return  to false ; 
        } 
    }, 
    // check whether the string is valid zip code 
    isPostcode : function (STR) {
         // start number is not 0, then 5 digits [1-9] \ {D}. 5 
        the let REG = / ^ [1-9] \ {D} $. 5 / G;
         IF (reg.test (STR)) { 
            the console.log (STR + 'is valid zip code format' );
             return  to true ; 
        } the else {
            the console.log (STR + 'zip code format is not legal' );
             return  to false ; 
        } 
    }, 
    // check whether the string is valid ID number 
    isIDcard: function (STR) { 
        the let REG = / ^ (^ [l- 9] \ d {7} ( (0 \ d) | (1 [0-2])) (([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} $) | (^ [1-9] \ d {5} [1-9] \ d {3} ((0 \ d) | (1 [0-2])) (([0 | 1 | 2] \ d ) |. 3 [0-1]) ((\. 4 {D}) | \. 3} {D [Xx]) $) $ / ;
         IF (reg.test (STR)) { 
            the console.log (STR + 'is valid ID number " );
             return  to true ; 
        } the else { 
            the console.log (STR + 'ID number is not legal');
            return  to false ; 
        } 
    }, 
    // check whether the string is valid the URL 
    ISURL: function (STR) { 
        REG the let = / ^ HTTPS: \ / \ / (([A-zA-Z0-9 _-]) + (\? .)) *? (:? . \ d +) (\ / ((\)????? (? \) = & [a-zA-Z0-9 _-] (? \)) *) * $ / I;
         IF (reg.test (STR)) { 
            the console.log (STR + 'is legitimate the URL' );
             return  to true ; 
        } the else { 
            the console.log (STR + 'is not legal the URL' );
             return  to false ; 
        } 
    } 
    // check whether the string is valid date format yyyy-mm-dd 
    isDate: function (STR) { 
        the let REG = / ^ [1-2] [0-9] [0-9] [0-9] - [0-1 ] {0,1} [0-9] - [0-3] {0,1} [0-9] $ / ;
         IF (reg.test (STR)) { 
            the console.log (STR + 'is legitimate date format ' );
             return  to true ; 
        } the else { 
            the console.log (STR +' is not valid date format, YYYY-mm-dd ' );
             return  to false ; 
        } 
    }, 
    // check whether the string is valid IP address 
    isIP : function (STR) {
         // 1.1.1.1 four [0, 255]
        // first paragraph can not be 0 
        // each segment can not begin with 0 
        //
         // Native IP: 58.50.120.18 Jingzhou City, Hubei Province telecommunications 
        let reg = / ^ ([1-9 ] | [1-9] [0-9] | 1 [0-9] [0-9] | 2 [0-4] [0-9] |. 25 [0-5]) (\ ([0-9] | [1- . 9] [0-9] |. 1 [0-9] [0-9] | 2 [0-4] [0-9] | 25 [0-5])) {} $. 3 / GI;
         IF (REG .test (STR)) { 
            the console.log (STR + 'is legitimate IP address' );
             return  to true ; 
        } the else { 
            the console.log (STR + 'IP address is not legal' );
             return  to false ; 
        } 
    } 
} ;

Guess you like

Origin www.cnblogs.com/yang-2018/p/12074051.html