JS Common Regular Expressions

Source: Internet
Author: User
Tags validation examples domain name registration to domain

How to use: Var searzone= "Chinese character";
var re =new RegExp ("^[\u4e00-\u9fa5]{0,}$");//matching Chinese characters
Console.info (Re.test (Searzone)); JS Regular expression
Common Regular Expression Collections:

Verification Number: ^[0-9]*$
To verify N-bit numbers: ^\d{n}$
Verify that at least n digits: ^\d{n,}$
Verify the number of m-n bits: ^\d{m,n}$
Verify numbers starting with 0 and non 0: ^ (0|[ 1-9][0-9]*) $
Verify that there is a positive real number with two decimal places: ^[0-9]+ (. [ 0-9]{2})? $
Verify that there is a positive real number with 1-3 decimal places: ^[0-9]+ (. [ 0-9]{1,3})? $
Verify non-zero positive integers: ^\+? [1-9] [0-9]*$
To verify a nonzero negative integer: ^\-[1-9][0-9]*$
Validates non-negative integers (positive integers + 0) ^\d+$
Validates a non-positive integer (negative integer + 0) ^ ((-\d+) | ( 0+)) $
Verify the character with a length of 3: ^. {3}$
Validates a string consisting of 26 English letters: ^[a-za-z]+$
Validates a string consisting of 26 uppercase English letters: ^[a-z]+$
Validates a string consisting of 26 lowercase English letters: ^[a-z]+$
Validates a string consisting of a number and 26 English letters: ^[a-za-z0-9]+$
Validates a string consisting of a number, 26 letters, or underscores: ^\w+$
Verify user password: ^[a-za-z]\w{5,17}$ the correct format is: Start with a letter, the length is between 6-18, and can contain only characters, numbers, and underscores.
Verify that it contains ^%& ',; =?$\ ' characters:[^%& ', =?$\x22]+
Verify Kanji: ^[\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*)? $
Verify phone Number: ^ (\d3,4 |\d{3,4}-)? \d{7,8}$:--the correct format is: xxxx-xxxxxxx,xxxx-xxxxxxxx,xxx-xxxxxxx,xxx-xxxxxxxx,xxxxxxx,xxxxxxxx.
Verify your Social Security number (15-bit or 18-digit number): ^\d{15}|\d{}18$
Validation 12 months of the year: ^ (0?[ 1-9]|1[0-2]) $ correct format: "01"-"09" and "1" "12"
Verify one months of 31 days: ^ ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ The correct format is: 01, 09 and 1, 31.
Integer: ^-?\d+$
Non-negative floating-point number (positive floating point + 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 + 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 ^ (-?\d+) (\.\d+)? $

Other common validation Examples:
Directory:
1:js string length limit, judge character length, JS limit input, limit cannot be entered, textarea length limit
2.:js Judge Chinese characters, judge whether Chinese characters, can only input Chinese characters
3:js judge whether to enter English, only input English
4:js can only enter numbers, judge numbers, verify numbers, detect numbers, determine whether they are numeric, enter only numbers
5: Only English characters and numbers can be entered
6:JS Email verification, JS judgment email, mailbox/email format Verification
7:js character filtering, masking keywords
8:js password verification, password determination
2.1:js is not empty, empty, or not an object, judgment is empty, judgment is not empty
2.2: Compare values of two table items
2.3: The form can only be numbers and "_",
2.4: Table Item input value/length limit
2.5: Chinese/English/digital/e-mail address legality judgment
2.6: Limit the characters that the form item cannot enter
2.7 Control of the form's self-character
Universal Check function for 2.8:form text fields






1. Length Limit
<script>
function test ()
{
if (DOCUMENT.A.B.VALUE.LENGTH&GT;50)
{
Alert ("Cannot exceed 50 characters!") ");
Document.a.b.focus ();
return false;
}
}
</script>
<form name=a onsubmit= "return Test ()" >
<textarea name= "B" cols= "+" wrap= "VIRTUAL" rows= "6" ></textarea>
<input type= "Submit" name= "Submit" value= "Check" >
</form>

2. Can only be Chinese characters
<input onkeyup= "value="/oblog/value.replace (/[^u4e00-u9fa5]/g, ') ">

3. "Can only be in English
<script language=javascript>
function Onlyeng ()
{
if (! ( EVENT.KEYCODE&GT;=65&AMP;&AMP;EVENT.KEYCODE&LT;=90))
Event.returnvalue=false;
}
</script>

<input onkeydown= "Onlyeng (); >

4. Can only be a number
<script language=javascript>
function Onlynum ()
{
if (! ( (event.keycode>=48&&event.keycode<=57) | | (event.keycode>=96&&event.keycode<=105)))
Consider the numeric keys on the keypad
Event.returnvalue=false;
}
</script>

<input onkeydown= "Onlynum (); >

5. Only English characters and numbers
<input onkeyup= "value="/oblog/value.replace (/[w]/g, "')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^d]/g, ')) ">

6. Verify the fuel tank format
<script Language=javascript runat=server>
function Isemail (stremail) {
if (Stremail.search (/^w+ (-w+) | (. w+)) *@[a-za-z0-9]+ ((. | -) [a-za-z0-9]+] *. [a-za-z0-9]+$/)! =-1)
return true;
Else
Alert ("Oh");
}
</SCRIPT>
<input type=text onblur=isemail (this.value) >

7. Block Keywords (shield * * * and * * * * here)
<script language= "javascript1.2" >
function Test () {
if ((A.b.value.indexof ("* * *") = = 0) | | (A.b.value.indexof ("* * *") = = 0)) {
Alert (":)");
A.b.focus ();
return false;}
}
</script>
<form name=a onsubmit= "return Test ()" >
<input Type=text name=b>
<input type= "Submit" name= "Submit" value= "Check" >
</form>

8. Two times the same password is entered
<form method=post action= "" >
<input type= "Password" id= "INPUT1" >
<input type= "Password" id= "Input2" >
<input type= "button" value= "Test" onclick= "Check ()" >
</FORM>
<script>
function Check ()
{
With (document.all) {
if (Input1.value!=input2.value)
{
Alert ("false")
Input1.value = "";
Input2.value = "";
}
else Document.forms[0].submit ();
}
}
</script>


Shield Right button is cool
Oncontextmenu= "return false" ondragstart= "return false" onselectstart= "return False"
Add it to the body.


Two

2.1 The form item cannot be empty

<script language= "JavaScript" >
<!--
function Checkform ()
{
if (Document.form.name.value.length = = 0) {
Alert ("Please enter your name!");
Document.form.name.focus ();
return false;
}
return true;
}
-
</script>

2.2 Compare two table items for the same value

<script language= "JavaScript" >
<!--
function Checkform ()
if (document.form.PWD.value! = document.form.PWD_Again.value) {
Alert ("The password you entered two times is different!") Please re-enter. ");
Document. ADDUser.PWD.focus ();
return false;
}
return true;
}
-
</script>

2.3 Form items can only be numeric and "_" for phone/bank account verification, can be extended to domain name registration, etc.

<script language= "JavaScript" >
<!--
function Isnumber (String)
{
var Letters = "1234567890-"; can increase the input value by itself
var i;
var C;
if (String.charat (0) = = '-')
return false;
if (String.charat (string.length-1) = = '-')
return false;
for (i = 0; i < string.length; i + +)
{
c = String.charat (i);
if (Letters.indexof (c) < 0)
return false;
}
return true;
}
function Checkform ()
{
if (! Isnumber (Document.form.TEL.value)) {
Alert ("Your phone number is not legal!") ");
Document.form.TEL.focus ();
return false;
}
return true;
}
-
</script>


2.4 Table Item input value/length limit

<script language= "JavaScript" >
<!--
function Checkform ()
{
if (Document.form.count.value > | | Document.form.count.value < 1)
{
Alert ("Input value cannot be less than 0 greater than 100!");
Document.form.count.focus ();
return false;
}
if (document.form.message.value.length<10)
{
Alert ("Input text less than 10!");
Document.form.MESSAGE.focus ();
return false;
}
return true;
}
-
</script>

2.5 Chinese/English/digital/e-mail address legality judgment

<script language= "JavaScript" >
<!--

function Isenglish (name)//English value detection
{
if (Name.length = = 0)
return false;
for (i = 0; i < name.length; i++) {
if (Name.charcodeat (i) > 128)
return false;
}
return true;
}

function Ischinese (name)//Chinese value detection
{
if (Name.length = = 0)
return false;
for (i = 0; i < name.length; i++) {
if (Name.charcodeat (i) > 128)
return true;
}
return false;
}

function IsMail (name)//e-mail value detection
{
if (! Isenglish (name))
return false;
i = Name.indexof ("at");
j = Name Dot LastIndexOf ("at");
if (i = =-1)
return false;
if (i! = j)
return false;
if (i = = Name dot length)
return false;
return true;
}

function Isnumber (name)//numeric detection
{
if (Name.length = = 0)
return false;
for (i = 0; i < name.length; i++) {
if (Name.charat (i) < "0" | | name.charat (i) > "9")
return false;
}
return true;
}

function Checkform ()
{
if (! IsMail (Form). Email.value)) {
Alert ("Your email is illegal!") ");
Form. Email.focus ();
return false;
}
if (! isenglish (Form.name.value)) {
Alert ("English name is not legal!") ");
Form.name.focus ();
return false;
}
if (! Ischinese (Form.cnname.value)) {
Alert ("Chinese name is not legal!") ");
Form.cnname.focus ();
return false;
}
if (! isnumber (Form). Publiczipcode.value)) {
Alert ("ZIP code is illegal! ");
Form. Publiczipcode.focus ();
return false;
}
return true;
}
-
</SCRIPT>

2.6 Characters that limit the entry of a form item

<script language= "JavaScript" >
<!--

function contain (Str,charset)//string contains test function
{
var i;
for (i=0;i<charset.length;i++)
if (Str.indexof (Charset.charat (i)) >=0)
return true;
return false;
}

function Checkform ()
{
if (Contain (Document.form.NAME.value, "% () ><")) | | (Contain (Document.form.MESSAGE.value, "% () ><")))
{
Alert ("illegal character entered");
Document.form.NAME.focus ();
return false;
}
return true;
}
-
</script>

1. Check to see if a string is all made up of numbers
---------------------------------------
<script language= "Javascript" ><!--
function Checknum (str) {return Str.match (/d/) ==null}
Alert (Checknum ("1232142141"))
Alert (Checknum ("123214214A1"))
--></script>

2. How to determine if it is a character
---------------------------------------
if (/[^x00-xff]/g.test (s)) alert ("contains Chinese characters");
else alert ("All characters");

3. How to determine if a Chinese character is included
---------------------------------------
if (Escape (str). IndexOf ("%u")!=-1) alert ("contains kanji");
else alert ("All characters");

4. Mailbox Format Verification
---------------------------------------
Function Name: Chkemail
function: Check if it is an email Address
Parameter description: The string to check
Return value: 0: not 1: Yes
function Chkemail (a)
{var i=a.length;
var temp = a.indexof (' @ ');
var tempd = A.indexof ('. ');
if (Temp > 1) {
if ((i-temp) > 3) {
if ((I-TEMPD) >0) {
return 1;
}

}
}
return 0;
}

5. Digital format Verification
---------------------------------------
Function Name: Fucchecknum
Function Description: Check whether it is a digital
Parameter description: The number to check
Return value: 1 is a number, 0 is not a number
function Fucchecknum (NUM)
{
var i,j,strtemp;
strtemp= "0123456789";
if (num.length== 0)
return 0
for (i=0;i<num.length;i++)
{
J=strtemp.indexof (Num.charat (i));
if (j==-1)
{
Indicates that a character is not a number
return 0;
}
}
Description is a number
return 1;
}

6. Phone Number format verification
---------------------------------------
Function Name: Fucchecktel
Feature Description: Check if it's a phone number
Parameter description: The string to check
Return value: 1 is legal, 0 is illegal
function Fucchecktel (TEL)
{
var i,j,strtemp;
Strtemp= "0123456789-() #";
for (i=0;i<tel.length;i++)
{
J=strtemp.indexof (Tel.charat (i));
if (j==-1)
{
Indicates that there are characters that are not valid
return 0;
}
}
Description Legal
return 1;
}

7. Determine whether the input is a Chinese function
---------------------------------------
function Ischinese (s) {
var ret=true;
for (Var i=0;i<s.length;i++)
Ret=ret && (S.charcodeat (i) >=10000);
return ret;
}

8. A comprehensive function for judging the legality of user input
---------------------------------------
<script language= "JavaScript" >
Limit the number of digits of the input character to start
M is user input, n is the number of bits to limit
function Issmall (m,n)
{
if ((m<n) && (m>0))
{
return (false);
}
Else
{return (true);}
}

9. Determine if the password is entered consistently
---------------------------------------
function Issame (STR1,STR2)
{
if (STR1==STR2)
{return (true);}
Else
{return (false);}
}

10. Determine if the user name is a digital letter glide line
---------------------------------------
function Notchinese (str) {
var reg=/[^a-za-z0-9_]/g
if (Reg.test (str)) {
return (false);
}else{
return (true); }
}

2.8. Universal check function for form text fields
---------------------------------------
Function: Detects all input text that must be non-null, such as name, account number, email address, and so on.
This check is now only for text fields, and if you want to target other domain objects inside the form, you can change the judging criteria.

How to use: Add the title text to the text field you want to detect. The text is the Chinese name of the field in the prompt message that you want to prompt to the user. For example, to detect user names
HTML as follows <input name= "txt_1" title= "name", of course, it is best to use visual tools such as Dreamweaver to edit the domain.
If you want to detect numeric type data, then unify the domain ID as sz.
JavaScript determines the date type is troublesome, so there is no date type verification program. Master can add.

Program Compare grass, just provide a train of thought. Initiate :)
Oh, by the way, the function call method:< form onsubmit= "return Dovalidate ()" >

function Dovalidate ()
{
FM=DOCUMENT.FORMS[0]//Detect only one form, if multiple can change the judging condition
for (i=0;i<fm.length;i++)
{
Detection and judging conditions, depending on the type can be modified
IF (FM. Tagname.touppercase () = = "INPUT" &&fm.type.touppercase () = = "TEXT" && (fm.title!= ""))

if (fm.value= "/blog/=" ")//
{
Str_warn1=fm.title+ "cannot be empty!";
alert (STR_WARN1);
Fm.focus ();
return false;
}
if (fm.id.toUpperCase () = = "SZ")//Digital Check
{
if (IsNaN (Fm.value))
{str_warn2=fm.title+ "format is not correct";
alert (STR_WARN2);
Fm.focus ();
return false;
}
}
}
return true;
}

JS Common Regular Expressions

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.