C # Regular Expression in JavaScript

Source: Internet
Author: User

With the application and development of the Internet, regular expressions play an increasingly important role in application systems! They are often the prosecutors of a system and a website, and play an intuitive and important role in information verification. For a regular expression, I only know what a logic expression is for. I only know that it is a mess of characters, which is profound and profound, it is used to test whether a lot of input information meets business requirements and meets business scenarios. At this time, its role is not much said. The application of regular expressions in C # language is summarized as follows, it may not be comprehensive. Let me add it slowly.


C # example: When we verify the data information of non-Chinese characters, we need to verify whether it is a Chinese character. If it is a Chinese character, we may provide a user-friendly reminder, at the same time, ensure that the Chinese character data information does not enter the background database to avoid dirty data. We can use the following method:


1. Verify Chinese characters. Verify that the input Chinese characters> return true;
}
>
20. verify Email address: "^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $ ";

21. Verify InternetURL: "^ http: // ([\ w-] + \.) + [\ w-] + (/[\ w -./? % & =] *)? $ ";

22. Verify the phone number: "^ (\ d {3, 4}-) | \ d {3.4 }-)? \ D {7,8} $ "correct format:" XXX-XXXXXXX "," XXXX-XXXXXXXX "," XXX-XXXXXXX "," XXX-XXXXXXXX"

"XXXXXXX" and "XXXXXXXX ";

23. Verify the ID card number (15 or 18 digits): "^ \ d {15} | \ d {18} $ ";

24. Verify 12 months of a year: "^ (0? [1-9] | 1 [0-2]) $ "the correct format is:" 01 "~ "09" and "1 "~ "12 ";

25. 31 days of verification for a month: "^ (0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) $ "the correct format is;" 01 "~ "09" and "1 "~ "31 ".


The 26 above are the regular expressions in the C # background of the asp.net project, such as [number], [date], [Password], [email], [Chinese character], [ID card], and [Phone number ].] and other common data format or Form validation rules, the following solutions are applicable to the html interface or JavaScript. Of course, some people think it is redundant. The above regular expressions can be used in JS or Jquery in the form of escape characters. I have a rough understanding and write it down for later reading.

1. you can only enter Chinese characters using regular expressions: onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5]/g, '')" // check whether the input is a Chinese character when the key is popped up.

Onbeforepaste = "cliPBoardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ u4E00-\ u9FA5]/g, '')" // verify Chinese characters before the user performs the paste operation

2. Use a regular expression to limit that only full-width characters can be entered:>
Onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ uFF00-\ uFFFF]/g, '')" // check whether it is a full-width character input before pasting.

3. Use a regular expression to limit that only numbers can be entered: onkeyup = "value = value. replace (/[^ \ d]/g,'') "// check that only numbers can be entered

Onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^ \ d]/g ,''))"

4. you can only enter numbers and English letters using regular expressions: onkeyup = "value = value. replace (/[\ W]/g, '')" // you can enter only numbers and English letters for verification.

Onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text'). replace (/[^ \ d]/g ,''))"


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.