JS Regular Expression Verification Encyclopedia (collection) _ Regular expressions

Source: Internet
Author: User
Tags getdate html tags

Reference URL http://hi.baidu.com/quiteuniverse/blog/item/9f3f043d46ad1e07bba16716.html

The following function calls the method:

function Check () 
{ 
var bb = document.getElementById ("txt_id"). VALUE;//TXT_ID is the ID alert for the text box  
(IsMobile (BB) );//ismobile represents any of the following function names}


HTML code:

<input type= "text" name= "TextField" id= "txt_id"/> <input type= "Submit" Name= " 
Submission" value= "submitted" onclick= "Check ()"/>

**************************

Determine if the input is a string consisting of a 0-9/A-z/A-Z

function Isalphanumber (str) { 


var result=str.match (/^[a-za-z0-9]+$/); 
if (result==null) return false; 
return true;
}

/**************************

Determines whether the input is a number--(numbers contain decimals)--

function Isnumber (str)
{return
 !isnan (str);
}

Determines whether the input is an integer

function Isint (str)
{
 var result=str.match (/^ (-|\+) \d+$/);
 if (result==null) return false;
 return true;
}

Determines whether the input is a valid long date format-

"Yyyy-mm-dd HH:MM:SS" | | "Yyyy/mm/dd HH:MM:SS"


function isdatetime (str)
{
 var result=str.match (/^ (\d{4}) (-|\/) (\d{1,2}) \2 (\ d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2}) $/);
 if (result==null) return false;
 var d= new Date (result[1], result[3]-1, result[4], result[5], result[6], result[7]);
 Return (D.getfullyear () ==result[1]&& (D.getmonth () +1) ==result[3]&&d.getdate () ==result[4]& &d.gethours () ==result[5]&&d.getminutes () ==result[6]&&d.getseconds () ==result[7]);

Check for Yyyy-mm-dd | | Date format for YYYY/MM/DD

function IsDate (str) {
 var result=str.match (/^ (\d{4}) (-|\/) (\d{1,2}) \2 (\d{1,2));
 if (result==null) return false;
 var d=new Date (Result[1], result[3]-1, result[4]);
 Return (D.getfullyear () ==result[1] && d.getmonth () +1==result[3] && d.getdate () ==result[4]);

Determine if the input is a valid e-mail message

function Isemail (str)
{
 var result=str.match (/^\w+ (-\w+) | ( \.\w+)) *\@[a-za-z0-9]+ (\.| -) [a-za-z0-9]+) *\. [a-za-z0-9]+$/);
 if (result==null) return false;
 return true;
}

Remove the trailing spaces of a string

function Trim (str) {return
 str.replace (/^\s*) | ( \s*$)/g, "");

Returns the actual length of the string, one Chinese character counts 2 lengths

function strlen (str) {return
 str.replace (/[^\x00-\xff]/g, "* *"). length;
}

Matching China ZIP code (6-bit)

function Ispostcode (str)
{
 var result=str.match (/[1-9]\d{5} (?!) \d)/);
 if (result==null) return false;
 return true;
}

Match domestic phone number (0511-4405222 or 021-87888822)

function Istell (str)
{
 var result=str.match (/\d{3}-\d{8}|\d{4}-\d{7}/);
 if (result==null) return false;
 return true;
}

A checksum is an integer (0-10000)

function Isint1 (str)
{
 var result=str.match (/^[0-9]$|^ ([1-9]) ([0-9]) {0,3}$|^10000$/);
 if (result==null) return false;
 return true;
}

Match Tencent QQ number

function isqq (str)
{
 var result=str.match (/[1-9][0-9]{4,}/);
 if (result==null) return false;
 return true;
}

Matching ID (15-bit or 18-bit)

function Isidcard (str)
{
 var result=str.match (/\d{15}|\d{18}/);
 if (result==null) return false;
 return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////

Verify that the text is empty

function Checknull (field,sval)
{
 if (Field.value = "")
  {
  alert ("Please fill in the + Sval +"!) ");
  Field.focus ();
  return false;
  }
  return true;
}

Mask input Characters

/***********************

Call Method:

In the text box, add onkeypress= "return Checkchar ()"

*************************/

function Checkchar ()
{ 
 var keycode = Event.keycode;
 if (!) ( keycode>=48&&keycode<=57))
 {return
  false;
 }
}

/************************************************************************************************************** *************

China Telephone Number Verification

Matching forms such as: 0511-4405222 or 021-87888822 or 021-44055520-555 or (0511) 4405222

Regular expression "((d{3,4}) |d{3,4}-)? d{7,8} (-d{3}) *"

China ZIP Code Verification

Matching forms such as: 215421

Regular expression "D{6}"

E-mail authentication

Matching forms such as: justali@justdn.com

Regular expression "w+" ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) * "

ID Card Verification

Matching forms such as: 15-bit or 18-bit ID card

Regular expression "d{18}|d{15}"

Common Digital Verification

Regular expressions

"D{n}" n is a specified length

Length range of "d{n,m}" N to M

Illegal character validation

Match illegal character Furu:< > &/' |

Regular expression [^<>&/| ' \]+

Date validation

Matching forms such as: 20030718,030718

Scope: 1900--2099

Regular Expression (((19) {1}| ( {1}) d{2}) |d{2}) [01]{1}d{1}[0-3]{1}d{1}

Matching regular expressions for Chinese characters: [\U4E00-\U9FA5]

Commentary: Matching Chinese is really a headache, with this expression will be easy to do

Match Double-byte characters (including Chinese characters): [^\x00-\xff]

Commentary: can be used to compute the length of a string (a double-byte character length meter 2,ascii 1 characters)

A regular expression that matches a blank row: \n\s*\r

Commentary: can be used to delete blank lines

Regular expression:< matching HTML tags (\s*?) [^>]*>.*?| < *? />

Commentary: The online version is too bad, the above can only match the part of the complex nested tags still powerless

A regular expression that matches the end-end whitespace character: ^\s*|\s*$

Commentary: A useful expression that can be used to delete white-space characters (including spaces, tabs, page breaks, and so on) at the end of a line at the beginning

Regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *

Commentary: Form validation is useful

Regular expressions that match URL URLs: [a-za-z]+://[^\s]*

Commentary: Online circulation of the version of the function is very limited, which can meet the basic requirements

Match account number is legal (beginning of letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$

Commentary: Form validation is useful

Match domestic phone number: \d{3}-\d{8}|\d{4}-\d{7}

Commentary: Match form such as 0511-4405222 or 021-87888822

Matching Tencent QQ Number: [1-9][0-9]{4,}

Commentary: Tencent QQ number starting from 10000

Match China ZIP Code: [1-9]\d{5} (?! \d)

Commentary: China postal code is 6 digits

Matching ID: \d{15}|\d{18}

Commentary: China's ID card is 15-or 18-digit

Matching IP address: \d+\.\d+\.\d+\.\d+

Commentary: Useful when extracting IP addresses

Extract the IP address from the information:

(\d+) \. (\d+) \. (\d+) \. (\d+)

To extract the Chinese mobile number in the information:

(*0*13\D{9)}

Chinese fixed phone number in the extraction information:

(\ (\d{3,4}\) |\d{3,4}-|\s)? \d{8}

Extract the Chinese telephone number (including mobile and fixed telephone) in the information:

(\ (\d{3,4}\) |\d{3,4}-|\s)? \d{7,14}

Extract the Chinese zip code in the information:

[1-9] {1} (\d+) {5}

Extract the Chinese ID number in the information:

\D{18}|\D{15}

To extract an integer from the information:

\d+

Extract floating-point numbers (that is, decimals) in the information:

(-?\d*) \.? \d+

Extract any number in the information:

(-?\d*) (\.\d+)?

Extract the Chinese string from the information:

[\u4e00-\u9fa5]*

Extract a double-byte string (kanji) from the information:

[^\x00-\xff]*

Extract the English string in the message:

\w*

To extract the network links in the information:

(h| H) (r| R) (e| E) (f| F) *= * (' | ')? (\w|\\|\/|\.) +('|"| *|>)?

To extract the mail address in information:

\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *

To extract picture links in information:

(s| S) (r| R) (c| C) *= * (' | ')? (\w|\\|\/|\.) +('|"| *|>)?

Match a specific number:

^[1-9]\d*$//Matching positive integer

^-[1-9]\d*$//matching negative integers

^-? [1-9]\d*$//matching integer

^[1-9]\d*|0$//matching nonnegative integer (positive integer + 0)

^-[1-9]\d*|0$//matching non positive integer (negative integer + 0)

^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$//matching positive floating-point numbers

^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*) $//matching negative floating-point number

^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) $//matching floating-point number

^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$//matching nonnegative floating-point number (positive floating-point number + 0)

^ (-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)) |0?\.0+|0$//matching non-positive floating-point numbers (negative floating-point number + 0)

Commentary: useful when dealing with large amounts of data, pay attention to corrections when applied

Match a specific string:

^[a-za-z]+$//Match a string of 26 English letters

^[a-z]+$//Match a string of 26 uppercase letters

^[a-z]+$//Match string consisting of 26 lowercase letters

^[a-za-z0-9]+$//Match a string of numbers and 26 English letters

^\w+$//Match A string of numbers, 26 English letters, or underscores

Commentary: Some of the most basic and commonly used expressions

First 4 line programs to protect JS code from being downloaded

Basic Regular Expression///////////////////

Non-null validation

function Notnull (str) {return (str!= "");}

Mail address Verification

function Checkemail (str) {

Message address Regular expression

 
  

Message address Regular expression


 
 

Verify mail address, return result

Return (Isemail1.test (str) &&isemail2.test (str)); }

ID Card Verification

 
 

Identity card Regular expression (15-bit)

 

Identity card Regular expression (18-bit)

 
 

Verify ID, return result

 
 

IP authentication

 
 

An IP regular expression

Ip= ' (25[0-5]|2[0-4]\\d|1\\d\\d|\\d\\d|\\d) '; 


Ipdot=ip+ ' \ n. ' Isipaddress=new RegExp (' ^ ' +ipdot+ipdot+ipdot+ip+ ' $ '); 

Verifying IP, returning results

 
  

Home page (URL) validation

 
  

Home Regular Expression

 
  

Validate Home page, return result

 
  

Whether the number

 

Validating and returning results

 
  

Whether integer

 

Validating and returning results

 

Whether the letter

 function Ischar (str) {ischaracter=/^[a-za-z]+$/; 

Validating and returning results

Return (Ischaracter.test (str)); } 

Basic pop-up Window///////////////////

function Checkboolean (bv,i,w) {if (bv==false) {Try{i.focus ();} catch (e) {} alert (w); return false; return true}

element and value judgement//////////////////////

Has been selected

function checkelement_selected (item,alert_str) { 

if (item.type== "Select-one") return Checkelement_notnull (item, ALERT_STR); 

if (alert_str.length==0) alert_str=item.title+ "for the required option!" ";

 Rt=false; if (item.length>0) {for (i=0;i<item.length;i++)

{

rt=rt| | item[i].checked;

} 

else 

{ 

rt=item.checked} return Checkboolean (RT,ITEM[0],ALERT_STR);

 

//Not empty

function Checkelement_notnull (a,alert_str,g) {v=a.value; w=alert_str; if (alert_str.length==0) w=a.title+ "cannot be empty!" "; 

Return (Checkvalue_notnull (v,a,w,g)); } function Checkvalue_notnull (v,i,w,g) {if (g!= "Not_trim") v=v.replace (/(^\s*) | ( \s*$)/g, ""); Bv=notnull (v); Return (Checkboolean (bv,i,w)); } 

//Legal Email

function Checkelement_isemail (a,alert_str,g) {v=a.value; w=alert_str; if (alert_str.length==0) w=a.title+ "cannot be empty!" ";

 Return (Checkvalue_isemail (v,a,w,g)); } 

   function Checkvalue_isemail (v,i,w,g) {if (g!= "Not_trim") v=v.replace (/(^\s*) | ( \s*$)/g, ""); Bv=checkemail (v); Return (Checkboolean (bv,i,w)); }

//Legal ID

function Checkelement_isidcard (a,alert_str,g) {v=a.value; w=alert_str; if (alert_str.length==0) w=a.title+ "cannot be empty!" "; 

Return (Checkvalue_isidcard (v,a,w,g)); }

function Checkvalue_isidcard (v,i,w,g) {if (g!= "Not_trim") v=v.replace (/(^\s*) | ( \s*$)/g, ""); Bv=checkidcard (v); 

Return (Checkboolean (bv,i,w)); } 

Legal

IP function Checkelement_isip (a,alert_str,g) {v=a.value; w=alert_str; if (alert_str.length==0) w=a.title+ "cannot be empty!" "; 

Return (Checkvalue_isip (v,a,w,g)); } function Checkvalue_isip (v,i,w,g) {if (g!= "Not_trim") v=v.replace (/(^\s*) | ( \s*$)/g, ""); Bv=checkip (v); Return (Checkboolean (bv,i,w)); } 

Verifying numbers

 function Checkelement_isnum (a,alert_str,g) {v=a.value; w=alert_str; if (alert_str.length==0) w=a.title+ "cannot be empty!" "; 

validating integers

function Checkelement_isint (a,alert_str,g) {v=a.value; w=alert_str; if (alert_str.length==0) w=a.title+ "cannot be empty!" "; 
Return (Checkvalue_isint (v,a,w,g)); } function Checkvalue_isint (v,i,w,g) {if (g!= "Not_trim") v=v.replace (/(^\s*) | ( \s*$)/g, ""); 
Bv=isint (v); Return (Checkboolean (bv,i,w)); } 

Verifying letters

function Checkelement_ischar (a,alert_str,g) {v=a.value; w=alert_str; if (alert_str.length==0) w=a.title+ "cannot be empty!" ";
 Return (Checkvalue_ischar (v,a,w,g)); } function Checkvalue_ischar (v,i,w,g) {if (g!= "Not_trim") v=v.replace (/(^\s*) | ( \s*$)/g, ""); Bv=ischar (v); Return (Checkboolean (bv,i,w)); } 

Legal Home

 function Checkelement_ishomepage (a,alert_str,g) {v=a.value; w=alert_str; if (alert_str.length==0) w=a.title+ "cannot be empty!" "; 
Return (Checkvalue_ishomepage (v,a,w,g)); } function Checkvalue_ishomepage (v,i,w,g) {if (g!= "Not_trim") v=v.replace (/(^\s*) | ( \s*$)/g, ""); Bv=checkhomepage (v); Return (Checkboolean (bv,i,w)); }

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.