Js uses regular expressions to verify mobile phone numbers in China

Source: Internet
Author: User

Js uses regular expressions to verify mobile phone numbers in China

This article mainly introduces how to verify mobile phone numbers in China using js and regular expressions, as well as examples of simple verification of mobile phone numbers using js and jQuery. It is very simple and practical. For more information, see.

Append an utils object that contains a mobile phone number verification function and a function for formatting and returning data.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

Var isChinaMobile =/^ 134 [0-8] \ d {7 }$ | ^ (? : 13 [5-9] | 147 | 15 [0-27-9] | 178 | 18 [2-478]) \ d {8} $ /; // mobile latest reply

Var isChinaUnion =/^ (? : 13 [0-2] | 145 | 15 [56] | 176 | 18 [56]) \ d {8} $/; // confirm with Unicom Weibo that no response has been made

Var isChinaTelcom =/^ (? : 133 | 153 | 177 | 18 [019]) \ d {8} $/; // The telecommunications team of section 1349 did not provide a reply, which is deemed to be nonexistent

Var isOtherTelphone =/^ 170 ([059]) \ d {7} $/; // other carriers

 

Var utils = {

CheckMobile: function (telphone ){

Telphone = this. trim (telphone );

If (telphone. length! = 11 ){

Return this. setReturnJson (false, 'no correct phone number' is detected ');

}

Else {

If (isChinaMobile. test (telphone )){

Return this. setReturnJson (true, 'mobile', {name: 'chinamobile '});

}

Else if (isChinaUnion. test (telphone )){

Return this. setReturnJson (true, 'connection', {name: 'chinaunion '});

}

Else if (isChinaTelcom. test (telphone )){

Return this. setReturnJson (true, 'telet', {name: 'chinatelcom '});

}

Else if (isOtherTelphone. test (telphone )){

Var num = isOtherTelphone.exe c (telphone );

Return this. setReturnJson (true, '', {name :''});

}

Else {

Return this. setReturnJson (false, 'no correct phone number' is detected ');

}

}

},

SetReturnJson: function (status, msg, data ){

If (typeof status! = 'Boolean' & typeof status! = 'Number '){

Status = false;

}

If (typeof msg! = 'String '){

Msg = '';

}

Return {

'Status': status,

'Msg ': msg,

'Data': data

};

}

}

Verify the mobile phone number of the 130-139,150-159,180-189 segment

?

1

2

3

4

5

6

7

8

<Script type = "text/javascript">

Var myreg =/^ (13 [0-9] {1}) | (15 [0-9] {1 }) | (18 [0-9] {1}) + \ d {8}) $ /;

If (! Myreg. test ($ ("# phone"). val ()))

{

Alert ('enter a valid mobile phone number! ');

Return false;

}

</Script>

The above code is debugged under jquery.

Jquery code is not required

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

Function validatemobile (mobile)

{

If (mobile. length = 0)

{

Alert ('enter your mobile phone number! ');

Document. form1.mobile. focus ();

Return false;

}

If (mobile. length! = 11)

{

Alert ('enter a valid mobile phone number! ');

Document. form1.mobile. focus ();

Return false;

}

 

Var myreg =/^ (13 [0-9] {1}) | (15 [0-9] {1 }) | (18 [0-9] {1}) + \ d {8}) $ /;

If (! Myreg. test (mobile ))

{

Alert ('enter a valid mobile phone number! ');

Document. form1.mobile. focus ();

Return false;

}

}

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.