Javascript learning notes (5) Regular Expressions

Source: Internet
Author: User
Regular Expressions are often used in web development and are mainly used to verify the format of user input data. Frequently Used metacharacters include :. searches for a single character, except for line breaks and line terminologies. w matches letters, Chinese characters, numbers, and underscores. s matches blank characters (including spaces and tabs). d matches numbers; match SyntaxHighlighter. all ();

Regular Expressions are often used in web development and are mainly used to verify the format of user input data.
Frequently Used metacharacters include:
. Find a single character, except for line breaks and line terminator;
W matches letters, Chinese characters, numbers, underscores, and other symbols;
S matches blank characters (including spaces and tabs );
D. Match the number;
Match at the beginning or end of a word;
Common quantifiers include:
^ N matches any string starting with n;
N $ matches any string ending with n;
N + matches any string containing at least one n;
N * matches any string containing zero or multiple n values;
N? Matches any string containing zero or one n;
N {X} matches the string containing X n sequences;
N {X, Y} matches strings that contain X or Y n sequences;
A simple example is used to verify the mobile phone number, phone number, and email address:
Javascript code:
View sourceprint? 01 function isMobile (){

02 var mobile = document. getElementById ("mobile_phone ");

03 var num = mobile. value;

04 var reg =/^ (13 [0-9] | 186 | 188 | 150 | 151 | 158 | 159 | 147) d {8} $ /;

05 if (num = ""){

06 alert ("Enter the complete mobile phone number ");

07 mobile. focus ();

08 return false;

09} else if (reg. test (num )){

10 alert ("the entered mobile phone number is in the correct format ");

11} else {

12 alert ("enter the correct 11-digit mobile phone number ");

13 mobile. focus ();

14 return false;

15}

16}

17

18 function isEmail (){

19 var email = document. getElementById ("email ");

20 var email_value = email. value;

21 if (email_value = ""){

22 alert ("Enter the complete email address ");

23 email. focus ();

24 return false;

25} else {

26 var reg =/^ [a-zA-Z0-9] (w) + @ (w) + (.) + (com | com.cn | net | cn | net.cn | org | biz | info | gov | gov.cn | edu | edu.cn) $ /;

27 if (reg. test (email_value )){

28 alert ("the entered email address format is correct ");

29} else {

30 alert ("enter the correct email format ");

31 email. focus ();

32 return false;

33}

34}

35}

36

37 function isPhone (){

38 var phone = document. getElementById ("phone ");

39 var phone_value = phone. value;

40 if (phone_value = ""){

41 alert ("Enter the complete landline number ");

42 phone. focus ();

43 return false;

44} else {

45 var reg =/^ [(]? 0d {2, 3} [)]? S * [-]? S * d {87989898} $/; // 010-01098989898 0712 (8989898) 23343434 010-All landline numbers in these formats are

46 if (reg. test (phone_value )){

47 alert ("the entered landline number is correct ");

48} else {

49 alert ("Incorrect landline number format ");

50 phone. focus ();

51 return false;

52}

53}

54}

Some HTML code:
View sourceprint? 01

02Mobile Machine

03

04

05

06

07Seat Machine

08

09

10

11

12Mail Box

13

14

15


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.