SoFunction
Updated on 2025-03-04

JavaScript regular expression verification code (recommended)

RegExp: is the abbreviation of regular expression.

Regular expressions describe the pattern object of a character. Regular expressions can be used to describe what to retrieve.

A simple pattern can be a single character. More complex patterns include more characters and can be used for parsing, format checking, substitution, and more.

//Discern whether the input content is emptyfunction IsNull(){ 
var str = ('str').(); 
if(==0){ 
alert('Sorry, the text box cannot be empty or space!');//Please change the "text box" to the attribute name you need to verify!} 
} 
//Judge whether the date type is YYYY-MM-DD format typefunction IsDate(){ 
var str = ('str').(); 
if(!=0){ 
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; 
var r = (reg); 
if(r==null) 
alert('Sorry, the date you entered is incorrectly format!'); //Please change "date" to the attribute name you need to verify!} 
} 
//Judge whether the date type is YYYY-MM-DD hh:mm:ss format typefunction IsDateTime(){ 
var str = ('str').(); 
if(!=0){ 
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; 
var r = (reg); 
if(r==null) 
alert('Sorry, the date you entered is incorrectly format!'); //Please change "date" to the attribute name you need to verify!} 
} 
//Judge whether the date type is the type of hh:mm:ss formatfunction IsTime() 
{ 
var str = ('str').(); 
if(!=0){ 
reg=/^((20|21|22|23|[0-1]\d)\:[0-5][0-9])(\:[0-5][0-9])?$/ 
if(!(str)){ 
alert("Sorry, the date you entered is incorrect!");//Please change "date" to the attribute name you need to verify!} 
} 
} 
//Discern whether the entered character is English letterfunction IsLetter() 
{ 
var str = ('str').(); 
if(!=0){ 
reg=/^[a-zA-Z]+$/; 
if(!(str)){ 
alert("Sorry, the English letter type you entered is incorrect in the format!");//Please change "English letter type" to the attribute name you need to verify!} 
} 
} 
//Judge whether the entered character is an integerfunction IsInteger() 
{ 
var str = ('str').(); 
if(!=0){ 
reg=/^[-+]?\d*$/; 
if(!(str)){ 
alert("Sorry, the integer type you entered is incorrect in the format!");//Please change the "integral type" to the attribute name you want to verify!} 
} 
} 
//Judge whether the input character is double precisionfunction IsDouble(val) 
{ 
var str = ('str').(); 
if(!=0){ 
reg=/^[-\+]?\d+(\.\d+)?$/; 
if(!(str)){ 
alert("Sorry, the double precision type you entered is incorrect in the format!");//Please change the "double precision type" to the attribute name you want to verify!} 
} 
} 
//Judge whether the input characters are: a-z, A-Z, 0-9function IsString() 
{ 
var str = ('str').(); 
if(!=0){ 
reg=/^[a-zA-Z0-9_]+$/; 
if(!(str)){ 
alert("Sorry, the string type you entered is incorrect in the format!");//Please change the "string type" to the attribute name you want to verify!} 
} 
} 
//Judge whether the entered character is Chinesefunction IsChinese() 
{ 
var str = ('str').(); 
if(!=0){ 
reg=/^[\u0391-\uFFE5]+$/; 
if(!(str)){ 
alert("Sorry, the string type you entered is incorrect in the format!");//Please change the "string type" to the attribute name you want to verify!} 
} 
} 
//Judge whether the input EMAIL format is correctfunction IsEmail() 
{ 
var str = ('str').(); 
if(!=0){ 
reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; 
if(!(str)){ 
alert("Sorry, the string type you entered is incorrect in the format!");//Please change the "string type" to the attribute name you want to verify!} 
} 
} 
//Judge whether the entered zip code (only six digits) is correctfunction IsZIP() 
{ 
var str = ('str').(); 
if(!=0){ 
reg=/^\d{6}$/; 
if(!(str)){ 
alert("Sorry, the string type you entered is incorrect in the format!");//Please change the "string type" to the attribute name you want to verify!} 
} 
} 
//Judge that the input number is not greater than a specific numberfunction MaxValue() 
{ 
var val = ('str').(); 
if(!=0){ 
reg=/^[-+]?\d*$/; 
if(!(str)){//Discern whether it is a numeric typeif(val>parseInt('123')) // "123" sets the maximum value for yourself{ 
alert('Sorry, the number you entered is out of range');//Please change the "number" to the attribute name you want to verify!} 
} 
} 
} 

Phone : /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/

Mobile : /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/

Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/

IdCard : /^\d{15}(\d{2}[A-Za-z0-9])?$/

QQ : /^[1-9]\d{4,8}$/

A special amount of some kind: /^((\d{1,3}(,\d{3})*)|(\d+))(\.\d{2})?$/ //Note: Except for the format "XXX XX,XXX XX,XXX.00"

// Provide the .trim() attribute for each JS verification method provided above.=function(){ 
return (/(^\s*)|(\s*$)/g, ""); 
}

Called:

&lt;input type="text" name="str" &gt;
&lt;input type="button" value=" Sure " onClick=""&gt; // Write the JS verification function you want to call in onClick&lt;script language="javascript" type="text/javascript"&gt;
var patterms = new Object();
//Verify IP = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/;
//Verify EMAIL = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
//Verify date format 2009-07-13 = /^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;
//Verification time format 16:55:39 = new RegExp("^([0-1]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$");
//Verify functionfunction verify(str,pat)
{
var thePat;
thePat = patterms[pat];
if((str))
{
return true;
}
else
{
return false;
}
}
//testalert(verify("asidycom@","email")+","+verify("192.168.1.1","ip")+
","+verify("16:55:39","time")+","+verify("2009-07-13","date")+","+verify("192.168","ip"));

Verification number: ^[0-9]*$

Verify n-bit number: ^\d{n}$

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

Verify the number of m-n bits: ^\d{m,n}$

Verify numbers beginning with zero and non-zero: ^(0|[1-9][0-9]*)$

Verify that there are two decimal places: ^[0-9]+(.[0-9]{2})?$

Verify that positive real numbers with 1-3 decimal places: ^[0-9]+(.[0-9]{1,3})?$

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

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

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

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

Verify characters with length 3: ^.{3}$

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

Verify a string composed of 26 capital English letters: ^[A-Z]+$

Verify a string composed of 26 lowercase English letters: ^[a-z]+$

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

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

Verifying usernames or nicknames is often used: ^[\u4e00-\u9fa5A-Za-z0-9-_]*$ Only in Chinese and English, numbers, underscores, and minus numbers.

Verify user password:^[a-zA-Z]\w{5,17}$ The correct format is: start with a letter, length between 6-18, and can only contain characters, numbers and underscores.

Verify that it contains characters such as ^%&',;=?$\": [^%&',;=?$\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-XXXXXXXXX, XXXXXXXXXXX, XXXXXXXXXXXX.

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

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

Verify 31 days of one month: ^((0?[1-9])|((1|2)[0-9])|30|31)$ The correct formats are: 01, 09 and 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+)?$

Due to the continuous update of mobile phone number segments, the previous regular expressions can no longer meet the needs. Rewrite this expression,

var regex = {
mobile: /^?([-]|[]|[]|[])[-]{}$/
}

Expression analysis:

"/" represents a regular expression.

"^" represents the beginning position of the string, and "$" represents the end position of the string.

"?" means matching one or zero characters in the previous one, so 0? here means that the mobile phone number can start with 0 or not start with 0.

The next part to verify the 11-digit mobile phone number starts with 13. Since there are 130-139, the optional interval is [0-9]. The number starting with 15 does not have 154, so there is no 4 in []. Of course, it can also be written as [0-35-9]. The numbers opened below 18 and 14 are the same as above.

The brackets represent a subexpression, and there are 4 optional branches in it, which are distinguished by "|" respectively. In the regular, the priority of "|" is the lowest. Here, each branch matches 3 characters (one [] can only match one character, and the meaning is optional), that is, the first 3 digits of the mobile phone number, and there are 8 digits afterwards that need to be matched, which can be any character of 0-9, so it is "[0-9]{8}". The number in {} represents the number matching the previous characters.

The above content is the JavaScript regular expression verification code shared by the editor with you. I hope it will be helpful to you. If you want to know more exciting content, please pay attention to my website. Thank you!