JavaScript form Validation-uncovering the veil _javascript of regular expressions

Source: Internet
Author: User

Recommended reading: JavaScript form verification length

JavaScript Form validation-Submitting a form

JavaScript form Validation-the first recognition of regular expressions

In the previous article to introduce JavaScript form validation-the first regular expression, this article to introduce JavaScript form verification-open the veil of the regular expression, see the full text of the details.

Match the corresponding character type with a meta character

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

A first-level meta character is used in regular expressions to connect letters to numbers

"." matches any character except for line breaks

"\d" matches any numeric character

"\w" matches any alphabetic or numeric character

"\s" matches spaces

The "^" string should start with a pattern

The "$" string should end in pattern

A meta character is not just a literal character, it is a symbol used to construct a regular expression

Example: Here are three characters

"A", "7", "%"

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

/^\d/can match "7"

/\d/can match "7"

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

But what about strings that contain multiple characters?

"2nite", "007", "Catch22",

/^\d/can be matched to "2nite", "007" (starting with numbers)

/\d\d\d/can match "007" (3 digits in a row)

/^cat/can Match "Catch22" (beginning with cat characters)

/\d\d$/can Match "catch22" (must end with two digits)

For example: Matching U.S. ZIP Code, ZIP code format for #####-####

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

Specifies the number of occurrences of a character with a qualifier

Quantifiers are used for the number of times a control's child mode appears in a regular expression

The qualifier is match either, the qualifier is applied in the child mode, and the number of times the child mode appears in the pattern

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

The child mode before the "+" qualifier must appear 1 or more times

“?” The child mode before the qualifier must appear 0 or 1 times

The child mode before the ' {n} ' qualifier must appear exactly n times

"()" collection character or/and meta character, which becomes a child mode

The same is the case for a ZIP code

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

As you can see, a qualifier is more accurate than an expression with only metacharacters.

/\w*/matches any alphanumeric character, including an empty string

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

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

* When you want to match a character with a special meaning in a regular expression, you can use the backslash

such as matching $:\$*

Validating data with regular expressions

A regular expression in JavaScript is represented by a RegExp object that contains a key –test () method that uses regular expressions to validate data, and it checks whether a specified pattern exists in the string

Cases:

Copy Code code as follows:

var regex=/^\d{5}$/;//is a regular expression that matches a 5-digit postal code;

Regular Expression object literal automatically builds RegExp object

if (!regex.test (inputfiled.value)) 
{ 
/// 
/////////////////////////////// 
return False 
}

Code case

Next, write a method that is designed to validate string formatting

Regex Regular Expression
//inputstr a string that needs to be validated
//helptext a span label that provides informational hints
//helpmessage message content
/
function Validateregexp (regex,inputstr,helptext,helpmessage)
{
if (!regex.test (INPUTSTR))
{
if (helptext! =null)
helptext.innerhtml=helpmessage;
return false;
}
else{
if (helptext!=null)
helptext.innerhtml= "";
}
return true;
}
function Validatedate (inputfild,helptext)
{
if!validatenonempty (inputfild,helptext)/Check the parameter is
non-null {return
false;
}
Return Validateregexp (/^\d{2}\/\d{2}\/\d{4}$/,inputfild,helptext, "Please enter the correct date format");/Call the regular authentication method
}

Well, this concludes this article, thank you to the cloud-Habitat community website support!

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.