SoFunction
Updated on 2025-04-06

JS regular expression verification collection (collected)

Quote URL/quiteuniverse/blog/item/

The following function call method:   

function check() 
{ 
var bb = ("txt_id").value;//txt_id is the ID of the text boxalert(ismobile(bb));//ismobile represents any of the following function names }

HTML code:

<input type="text" name="textfield"  /> 
<input type="submit" name="Submit" value="submit" onclick="check()" />

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

/// Determine whether the input is a string composed of 0-9 / A-Z / a-z

function isalphanumber(str){ 


var result=(/^[a-zA-Z0-9]+$/); 
if(result==null) return false; 
return true;
}

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

// Determine whether the input is a number-(number contains decimals)-

function isnumber(str)
{
 return !isNaN(str);
}

// Determine whether the input is an integer

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

// Determine 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=(/^(\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 (()==result[1]&&(()+1)==result[3]&&()==result[4]&&()==result[5]&&()==result[6]&&()==result[7]);
}

// Check whether it is YYYY-MM-DD || YYYY/MM/DD date format

function isdate(str){
 var result=(/^(\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 (()==result[1] && ()+1==result[3] && ()==result[4]);
}

// Determine whether the input is valid for email

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

// Remove the spaces at the beginning and end of the string

function trim(str){
 return (/(^\s*)|(\s*$)/g, "");
}

// Return the actual length of the string, one Chinese character calculates 2 lengths

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

//Match the Chinese postal code (6 digits)

function ispostcode(str)
{
 var result=(/[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=(/\d{3}-\d{8}|\d{4}-\d{7}/);
 if(result==null) return false;
 return true;
}

//Check whether it is an integer (0-10000)

function isint1(str)
{
 var result=(/^[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=(/[1-9][0-9]{4,}/);
 if(result==null) return false;
 return true;
}

//Match ID card (15 or 18)

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

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

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

//Check whether the text is empty

function checknull(field,sval)
{
 if ( =="")
  {
  alert("Please fill in" + sval + "!");
  ();
  return false;
  }
  return true;
}

//Mask input characters

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

Calling method:

Add onkeypress="return checkChar()" to the text box

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

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

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

China Telephone Number Verification

Matching forms are: 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 Postal Code Verification

Matching form is as follows: 215421

Regular expression "d{6}"

Email Verification

Matching form is: justali@

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

Identity card verification

Matching form: 15-bit or 18-bit ID card

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

Commonly used digital verification

Regular expressions

"d{n}" n is the specified length

"d{n,m}" length range from n to m

Illegal character verification

Match illegal characters such as: < > & / ' |

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

Date verification

Matching form is as follows: 20030718,030718

Range: 1900--2099

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

Regular expression matching Chinese characters: [\u4e00-\u9fa5]

Comment: Matching Chinese is really a headache, it's easy to do with this expression

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

Comment: It can be used to calculate the length of a string (a double-byte character length meter 2, ASCII character meter 1)

Regular expression matching blank lines:\n\s*\r

Comment: Can be used to delete blank lines

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

Comment: The version circulating online is too bad, and the above one can only match the part, and it is still powerless to use complex nested markers.

Regular expression matching the beginning and end blank characters: ^\s*|\s*$

Comment: It can be used to delete whitespace characters at the beginning and end of the line (including spaces, tabs, page breaks, etc.), a very useful expression

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

Comment: It is very practical when verifying the form

Regular expression matching URL: [a-zA-z]+://[^\s]*

Comment: The functions of the version circulating online are very limited, and the above can basically meet the needs

Match whether the account is legal (beginning with letters, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$

Comment: It is very practical when verifying the form

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

Comment: Matching forms are as follows: 0511-4405222 or 021-87888822

Match Tencent QQ number: [1-9][0-9]{4,}

Comment: Tencent QQ number starts at 10,000

Match the Chinese postal code: [1-9]\d{5}(?!\d)

Comment: China's postal code is 6 digits

Match ID card: \d{15}|\d{18}

Comment: China's ID card is 15 or 18 digits

Match IP address: \d+\.\d+\.\d+\.\d+

Comment: It is useful when extracting IP addresses

The IP address in the extract information:

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

Extract Chinese mobile phone numbers from the information:

(86)*0*13\d{9}  

Chinese landline number extracted from the information:

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

Extract Chinese phone numbers from the information (including mobile and landline phones):

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

Chinese postal codes in the extract information:

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

Extract the Chinese ID number in the information:

\d{18}|\d{15}  

Extract integers in the information:

\d+  

Extract floating point numbers (i.e. decimals) from the information:

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

Extract any number in the information:

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

Extract Chinese strings from the information:

[\u4e00-\u9fa5]*  

Extract the double-byte string (Chinese characters):

[^\x00-\xff]*

Extract the English string from the information:

\w*

Extract network links from information:

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

Email address in the extract message:

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

Image links in the extract information:

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

Match specific numbers:

^[1-9]\d*$  //Match positive integers

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

^-?[1-9]\d*$ //Match integers

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

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

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

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

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

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

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

Comment: It is useful when processing large amounts of data, and be careful to correct it when applying it in detail.

Match a specific string:

^[A-Za-z]+$//Match a string composed of 26 English letters

^[A-Z]+$//Match a string composed of 26 English letters capitalizations

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

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

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

Comment: Some of the most basic and most commonly used expressions

////////////////////////////// The first 4 lines of the program are used to protect the js code from being downloaded

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

//Non-empty verification

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

//Email address verification

function checkEmail (str) {

// Email address regular expression

isEmail1=/^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+$/; 

// Email address regular expression


isEmail2=/^.*@[^_]*$/; 

//Verify the email address and return the result

return ((str)&&(str)); }

//Identity card verification

function checkIDCard (str) { 

//Identity card regular expression (15 bits)

 isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/; 

//Identity card regular expression (18 bits)

isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/; 

//Verify the ID card and return the result

return ((str)||(str)); } 

//IP verification

function checkIP (str) { 

//IP regular expression

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


IPdot=IP+'\\.'; isIPaddress=new RegExp('^'+IPdot+IPdot+IPdot+IP+'$'); 

//Verify IP and return the result

return ((str)); } 

//Home page (website) verification

function checkHomepage (str) { 

//Home page regular expression

//isHomepage=/^\w+([\.\-]\w)*$/; isHomepage=/^\w+(\.\w+)+\.\w+$/; 

//Verify the home page and return the result

return ((str)); } 

//Is it a number

 function isNum (str) { //isNumber=/^([1-9]\d*(\.\d+)?)|(\d+(\.\d+))$/; isNumber=/^\d+(\.\d+)?$/; 

//Verify and return the result

return ((str)); } 

//Is it an integer

 function isInt (str) { isInteger=/^\d+$/; 

//Verify and return the result

 return ((str)); } 

//Is the letter

 function isChar (str) { isCharacter=/^[A-Za-z]+$/; 

//Verify and return the result

return ((str)); } 

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

            function checkBoolean(bv,i,w) { if(bv==false) { try{();}catch(e){} alert(w); return false; } return true } 

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

Selected

function checkElement_selected(item,alert_str) { 

if(=="select-one")return checkElement_NotNull(item,alert_str); 

if(alert_str.length==0)alert_str=+"A must-have!";

 rt=false; if(&gt;0) { for(i=0;i&lt;;i++)

{

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

} } 

else 

{ 

rt= } return checkBoolean(rt,item[0],alert_str); return true;

 }

 // Not empty

function checkElement_NotNull(a,alert_str,g) { v=; w=alert_str; if(alert_str.length==0)w=+"Can't be empty!"; 

return(checkValue_NotNull(v,a,w,g)); } function checkValue_NotNull(v,i,w,g) { if(g!="NOT_TRIM")v=(/(^\s*)|(\s*$)/g, ""); bv=NotNull(v); return(checkBoolean(bv,i,w)); } 

   // Legal email address

function checkElement_IsEmail(a,alert_str,g) { v=; w=alert_str; if(alert_str.length==0)w=+"Can't be empty!";

 return(checkValue_IsEmail(v,a,w,g)); } 

   function checkValue_IsEmail(v,i,w,g) { if(g!="NOT_TRIM")v=(/(^\s*)|(\s*$)/g, ""); bv=checkEmail(v); return(checkBoolean(bv,i,w)); }

 // Legal ID card

function checkElement_IsIDCard(a,alert_str,g) { v=; w=alert_str; if(alert_str.length==0)w=+"Can't be empty!"; 

return(checkValue_IsIDCard(v,a,w,g)); }

function checkValue_IsIDCard(v,i,w,g) { if(g!="NOT_TRIM")v=(/(^\s*)|(\s*$)/g, ""); bv=checkIDCard(v); 

return(checkBoolean(bv,i,w)); } 

// Legal

IP function checkElement_IsIP(a,alert_str,g) { v=; w=alert_str; if(alert_str.length==0)w=+"Can't be empty!"; 

return(checkValue_IsIP(v,a,w,g)); } function checkValue_IsIP(v,i,w,g) { if(g!="NOT_TRIM")v=(/(^\s*)|(\s*$)/g, ""); bv=checkIP(v); return(checkBoolean(bv,i,w)); } 

// Verify the number

 function checkElement_IsNum(a,alert_str,g) { v=; w=alert_str; if(alert_str.length==0)w=+"Can't be empty!"; 
return(checkValue_IsNum(v,a,w,g)); } function checkValue_IsNum(v,i,w,g) { if(g!="NOT_TRIM")v=(/(^\s*)|(\s*$)/g, ""); bv=isNum(v); return(checkBoolean(bv,i,w)); } 

// Verify integers

function checkElement_IsInt(a,alert_str,g) { v=; w=alert_str; if(alert_str.length==0)w=+"Can't be empty!"; 
return(checkValue_IsInt(v,a,w,g)); } function checkValue_IsInt(v,i,w,g) { if(g!="NOT_TRIM")v=(/(^\s*)|(\s*$)/g, ""); 
bv=isInt(v); return(checkBoolean(bv,i,w)); } 

// Verify letters

function checkElement_IsChar(a,alert_str,g) { v=; w=alert_str; if(alert_str.length==0)w=+"Can't be empty!";
 return(checkValue_IsChar(v,a,w,g)); } function checkValue_IsChar(v,i,w,g) { if(g!="NOT_TRIM")v=(/(^\s*)|(\s*$)/g, ""); bv=isChar(v); return(checkBoolean(bv,i,w)); } 

// Legal homepage

 function checkElement_IsHomepage(a,alert_str,g) { v=; w=alert_str; if(alert_str.length==0)w=+"Can't be empty!"; 
return(checkValue_IsHomepage(v,a,w,g)); } function checkValue_IsHomepage(v,i,w,g) { if(g!="NOT_TRIM")v=(/(^\s*)|(\s*$)/g, ""); bv=checkHomepage(v); return(checkBoolean(bv,i,w)); }