JavaScript Regular Expressions (Summary) and javascript Regular Expressions

Source: Internet
Author: User
Tags valid email address

JavaScript Regular Expressions (Summary) and javascript Regular Expressions

This article is a small series of articles on the js Regular Expression materials, the main purposes are two: one: self-review summary. Ii. Sharing facilitates searching
. If you are interested in js regular expressions, you can learn them together. If you do not have any knowledge points to write, you are welcome to add them and learn and make progress together.

Verification number: ^ [0-9] * $

Verify the n-digit number: ^ \ d {n} $

Verify at least n digits: ^ \ d {n,} $

Verify m-n digits: ^ \ d {m, n} $

Verify the number starting with zero or zero: ^ (0 | [1-9] [0-9] *) $

Verify the positive number of two decimal places: ^ [0-9] + (. [0-9] {2 })? $

Verify the positive number of 1-3 decimal places: ^ [0-9] + (. [0-9] {1, 3 })? $

Verify a non-zero positive integer: ^ \ +? [1-9] [0-9] * $

Verify a non-zero negative integer: ^ \-[1-9] [0-9] * $

Verify non-negative integer (positive integer + 0) ^ \ d + $

Verify non-positive integer (negative integer + 0) ^ (-\ d +) | (0 +) $

3 characters for verification: ^. {3} $

Verify A string consisting of 26 English letters: ^ [A-Za-z] + $

Verify a string consisting of 26 uppercase letters: ^ [A-Z] + $

Verify a string consisting of 26 lower-case letters: ^ [a-z] + $

Verify a string consisting of digits and 26 English letters: ^ [A-Za-z0-9] + $

Verify a string consisting of digits, 26 English letters, or underscores: ^ \ w + $

Verify User Password: ^ [a-zA-Z] \ w {5, 17} $ the correct format is: it must start with a letter and be between 6 and 18 characters. It can only contain characters, numbers, and underscores.

Check whether ^ % & ',; =? $ \ "And other characters: [^ % & ',; =? $ \ X22] +

Verify Chinese characters: ^ [\ u4e00-\ u9fa5], {0,} $

Verify Email address: ^ \ w + [-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $

Verify InternetURL: ^ http: // ([\ w-] + \.) + [\ w-] + (/[\ w -./? % & =] *)? $; ^ [A-zA-z] +: // (w + (-w +) *) (. (w + (-w + )*))*(? S *)? $

Verification phone number: ^ (\ d {3, 4} \) | \ d {3, 4 }-)? \ D {7,8} $: -- the correct format is: XXXX-XXXXXXX, XXXX-XXXXXXXX, XXX-XXXXXXX, XXX-

XXXXXXXX, XXXXXXX, and XXXXXXXX.

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

12 months of verification: ^ (0? [1-9] | 1 [0-2]) $ the correct format is: "01"-"09" and "1" "12"

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

Integer: ^ -? \ D + $

Non-negative floating point number (Positive floating point number + 0): ^ \ d + (\. \ d + )? $

Positive floating point number ^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $

Non-Positive floating point number (negative floating point number + 0) ^ (-\ d + (\. \ d + )?) | (0 + (\. 0 + )?)) $

Negative floating point number ^ (-([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $

Floating Point Number ^ (-? \ D +) (\. \ d + )? $

×××××××××××××××××××××××××××××××××××××××××

The following are not tested. Please verify and use

1. Only numbers and English letters can be entered:

<input onkeyup="value=value.replace(/[\W]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" ID="Text1" NAME="Text1">

2. Only numbers can be entered:

<input onkeyup="value=value.replace(/[^\d]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" ID="Text2" NAME="Text2">

3. Only the fullwidth fields can be entered:

<input onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))" ID="Text3" NAME="Text3">

4. Only Chinese characters can be entered:

<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))" ID="Text4" NAME="Text4">

5. Email address verification:

Var regu = "^ ([0-9a-zA-Z] +) | ([0-9a-zA-Z] + [_. 0-9a-zA-Z-] * [0-9a-zA-Z] +) @ ([a-zA-Z0-9-] + [.]) + ([a-zA-Z] {2} | net | NET | com | COM | gov | GOV | mil | MIL | org | ORG | edu | EDU | int | INT |) $ "var re = new RegExp (regu); if (s. search (re )! =-1) {return true;} else {window. alert ("enter a valid and valid email address! ") Return false ;}

6. ID card:

"^ \ D {17} (\ d | x) $"

7.17 Regular Expressions

"^ \ D + $" // non-negative integer (positive integer + 0)
"^ [0-9] * [1-9] [0-9] * $" // positive integer
"^ (-\ D +) | (0 +) $" // non-positive integer (negative integer + 0)
"^-[0-9] * [1-9] [0-9] * $" // negative integer
"^ -? \ D + $ "// integer
"^ \ D + (\. \ d + )? $ "// Non-negative floating point number (Positive floating point number + 0)
"^ ([0-9] + \\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// Positive floating point number
"^ (-\ D + (\. \ d + )?) | (0 + (\. 0 + )?)) $ "// Non-Positive floating point number (negative floating point number + 0)
"^ (-([0-9] + \\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// negative floating point number
"^ (-? \ D +) (\. \ d + )? $ "// Floating point number
"^ [A-Za-z] + $" // A string consisting of 26 English letters
"^ [A-Z] + $" // a string consisting of 26 uppercase letters
"^ [A-z] + $" // a string consisting of 26 lowercase letters
"^ [A-Za-z0-9] + $" // string consisting of digits and 26 letters
"^ \ W + $" // a string consisting of a number, 26 English letters, or underscores
"^ [\ W-] + (\\. [\ w-] +) * @ [\ w-] + (\\. [\ w-] +) + $ "// email address
"^ [A-zA-z] +: // (\ w + (-\ w + )*)(\\. (\ w + (-\ w + )*))*(\\? \ S *)? $ "// Url

========================================================== =====

1. The dotted box when the cancel button is pressed

Add the attribute value hideFocus or HideFocus = true in input.

2. Read-only text box content

Add the attribute value readonly in input.

3. Prevent TEXT files from being cleared (style content can be referenced as a class)

 <INPUT style=behavior:url(#default#savehistory); type=text id=oPersistInput> 

4. Press ENTER to move the cursor to the next input box.

<input onkeydown="if(event.keyCode==13)event.keyCode=9" > 

5. It can only be Chinese (with flashing)

 <input onkeyup="value="/value.replace(/[" -~]/g,'')" onkeydown="if(event.keyCode==13)event.keyCode=9"> 

6. Only numbers are allowed (with flashing)

 <input onkeyup="value="/value.replace(/["^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"> 

7. Only numbers are allowed (no flashing)

 <input ime-mode:disabled" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false"> 

8. Only English and numbers can be entered (with flashing)

 <input onkeyup="value="/value.replace(/[\W]/g,"'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"> 

9. shielded Input Method

 <input type="text" name="url" ime-mode:disabled" onkeydown="if(event.keyCode==13)event.keyCode=9"> 

10. You can only enter numbers, decimal points, minus (-) characters (no flashing)

 <input onKeyPress="if (event.keyCode!=46 && event.keyCode!=45 && (event.keyCode<48 || event.keyCode>57)) event.returnValue=false"> 

11. Only two decimal places can be entered, and three decimal places (with flashing)

<input maxlength=9 onkeyup="if(value.match(/^\d{3}$/))value="/value.replace(value,parseInt(value/10))" ;value="/value.replace(/\.\d*\./g,'."')" onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 && event.keyCode!=45 || value.match(/^\d{3}$/) || /\.\d{3}$/.test(value)) {event.returnValue=false}" id=text_kfxe name=text_kfxe>

I will introduce you to the JavaScript Regular Expressions (Summary). I will add some useful knowledge in the future and thank you for your support for the help House website!

Articles you may be interested in:
  • Use regular expressions in js and C # For instance analysis to match a tag
  • Use regular expressions to format and highlight json strings
  • Analysis of single-row mode and multi-row mode instances in js Regular Expressions
  • Example of js Regular Expressions test () and exec ()
  • Add a thousands separator to a number using a Javascript Regular Expression
  • Js regular expressions match numbers, letters, underscores, and so on

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.