SoFunction
Updated on 2025-03-02

Javascript Form Verification - Unveiling the Veil of Regular Expressions

Recommended reading:Javascript form verification length

Javascript form validation - Submit form

Javascript form verification - first understanding of regular expressions

In the previous article, I introduced it to youJavaScript form verification - first understanding of regular expressions, This article introduces Javascript's form verification - unveiling the veil of regular expressions. For details, please see the full text.

Match the corresponding character type with meta characters

Creating a regular expression is a bit like creating a string literal, but the regular expression appears in a pair of "/"

A first-level metacharacter is used in regular expressions to connect letters and numbers.

"." matches any character except line breaks

"\d" matches any numerical character

"\w" matches any letter or numeric character

"\s" matches space

"^" string must start with mode

"$" string must end in mode

Metacharacter does not only represent a literal character, it is a symbol used to construct regular expressions.

Example: There are three characters here

“A”,”7”,”%”

/\w/can match "A", "7"

/^\d/can match "7"

/\d/can match "7"

/./ can match "A", "7", "%"

But what should I do if a string containing multiple characters?

“2nite”,”007”,”catch22”,

/^\d/ can match "2nite", "007" (both numbers are all started)

/\d\d\d/ can match "007" (there are 3 numbers in a row)

/^cat/ can match "catch22" (starting with the cat character)

/\d\d$/ can match "catch22" (must end with two numbers)

For example: match the US zip code, the zip code format is ####-########################################################################################################################################################################################################################################

/^\d\d\d\d\d-\d\d\d\d$/

Use qualifiers to specify the number of characters to appear

Qualifiers are used to the number of times a control sub-mode appears in a regular expression

The qualifier is a sub-mode before it. The qualifier is applied to the sub-mode and controls the number of times the sub-mode appears in the mode.

The sub-mode before the "*" qualifier must appear 0 or more times

The sub-mode before the "+" qualifier must appear once or more times

The sub-mode before the "?" qualifier must appear 0 or 1 time

The sub-mode before the "{n}" qualifier must appear exactly N times

"()" set characters or / and meta characters become sub-mode

Take the postal code as an example

/^\d{5}-\d{4}$/

It can be seen that expressions with qualifiers are more accurate than expressions with only meta characters.

/\w*/Match any alphanumeric characters, including empty strings

/.+/Match a string that appears more than once (used to match non-non-empty strings)

/(Hot)??Donuts/ can match Hot or Donuts

* When you want to match characters with special meaning in regular expressions, you can use a backslash

For example, match $:\$*

Verify data with regular expressions

The regular expression in JavaScript is represented by a RegExp object, which contains the key to verifying data using regular expressions – test() method, which checks whether there is a specified pattern in the string.

example:

Copy the codeThe code is as follows:

var regex=/^\d{5}$/;// Regular expression matching 5-digit postal code;

Regex object literal automatically combines RegExp object

if(!()) 
{ 
//Calling text method on regular expression//If the requirements of the regular expression are met, return true//If the requirements of regular expressions are not met, return false}

Code Case

Next, write a method specifically used to verify string format

//regex regular expression//InputStr that needs to be verified// helpText provides a span tag that prompts information//helpMessage prompt information content//
function validateRegExp(regex,inputStr,helpText,helpMessage)
{
if(!(inputStr))
{
if(helpText!=null)
=helpMessage;
return false;
}
else{
if(helpText!=null)
="";
}
return true;
}
function validateDate(inputFild,helpText)
{
if(!validateNonEmpty(inputFild,helpText)//Check the parameters are non-empty{
return false;
}
return validateRegExp(/^\d{2}\/\d{2}\/\d{4}$/,inputFild,helpText,"Please enter the correct date format");//Calling regular verification method}

OK, this article ends here. Thank you for your support for my website!