Regular expression to verify the phone number and email verification

1. Implement regular expressions phone verification.

Common phone number is 11

The first three bits represent regions and operators

Regular expressions reg ^ 1 represents the beginning with 1 

        (3 [0-9]) denotes the number 2 is 3, with the back of data from 3 0-9 so here there are two numbers

        \ D {8} followed by eight numbers

Because Unicom paragraph numbers, mobile numbers section, so use the telecommunications segment number | or operator.

If you do not understand can be shortened to let reg = / ^ [1] [3,4,5,7,8,9] [0-9] {9} $ /;

           ^ [1] In the beginning of the number 1, [3,4,5,7,8,9], and the second number is the one set, [0-9], the range of 0 to 9, 9} {match 9, $ end.

 

  function isPhoneNumber(phoneNum){
            let reg=/^1(3[0-9]|4[5,7]|5[0,1,2,3,4,5,6,7,8,9]|6[2,5,6,7]|7[0,1,7,8]|8[0-9]|9[1,8,9])\d{8}$/;
            return reg.test(phoneNum);
        }
        console.log(isPhoneNumber(18212345678));

2. implement regular expression-mail verification

   Simple implementation 

 function  isEmail(email){
            let reg=/^\w+@[a-z0-9]+\.[a-z]{2,4}$/;
            return reg.test(email);
        }
        console.log(isEmail("[email protected]"));

 Complex realization

  Reg in the regular expression [a-zA-Z0-9 _.-] represents the first range comprises a begin underscore attention point, decimal point, minus.

  
    let reg = / ^ [a-zA-Z0-9 _.-] + @ [a-zA-Z0-9 -] + (\. [a-zA-Z0-9 -] +) * \. [a- zA-Z0-9] {2,6} $ /;
 

Guess you like

Origin www.cnblogs.com/linxim/p/11723181.html