Javascript form verification-uncover the veil of regular expressions and the secrets of Xiaomi 5
Recommended: Javascript Form Verification Length
Javascript form verification-submit a form
Form Verification in Javascript-Regular Expressions first recognized
In the previous article, we introduced javascript form verification-Regular Expression Recognition. This article introduces Javascript form verification-uncover the veil of regular expressions. For details, see the full text.
Match the corresponding character type with metacharacters
Creating a regular expression is a bit like creating a string literal, but the regular expression appears in a pair "/"
The first-level metacharacters are used in regular expressions to connect letters and numbers.
"." Matches any character, except line breaks
"\ D" matches any number characters
"\ W" matches any letter or Digit
"\ S" matches Spaces
The "^" string must start with the mode.
The "$" string must end in the mode.
Metacharacters not only represent a literal character, but are symbols used to build regular expressions.
For example, there are three characters
"A", "7", "%"
/\ W/can match "A", "7"
/^ \ D/can match "7"
/\ D/can match "7"
// Matching "A", "7", "%"
But what should I do if a string contains multiple characters?
"2nite", "007", "catch22 ",
/^ \ D/can match with "2nite" and "007" (numbers at the beginning)
/\ D/can match "007" (a row contains three numbers)
/^ Cat/can match "catch22" (starting with cat)
/\ D $/matched "catch22" (must end with two numbers)
For example, if the URL matches the U.S. zip code in the format #####-####
/^ \ D-\ d $/
Use a qualifier to specify the number of times a character appears
The number of times the control subpattern appears in a regular expression.
The Delimiter is in sub-mode. The Delimiter is applied to sub-mode and controls the number of times that sub-mode appears in mode.
The submode before the "*" qualifier must appear 0 or multiple times
The submode before the "+" qualifier must appear once or multiple times.
"?" The submode before the qualifier must appear 0 or 1 time
The submode before the "{n}" qualifier must appear exactly N times
The "()" character set or/or metacharacters are sub-modes.
The postal code is also used as an example.
/^ \ D {5}-\ d {4} $/
It can be seen that the expressions with a qualifier are more accurate than those with only metacharacters.
/\ W */match any alphanumeric characters, including empty strings
//. +/Match a string that appears more than once (used to match a non-empty string)
/(Hot )?? Donuts/can match Hot or Donuts
* When you want to match special characters in a regular expression, you can use a backslash.
For example, match $: \ $ *
Use regular expressions to verify data
The Regular Expression in JavaScript is represented by a RegExp object, which contains the key-test () method for verifying data using a regular expression. It checks whether a specified pattern exists in the string.
Example:
Copy codeThe Code is as follows:
Var regex =/^ \ d {5} $/; // match the regular expression of the 5-digit postal code;
The RegExp object is automatically merged with the regular expression object literal.
If (! Regex. test (inputFiled. value) {// call the text method on the regular expression // if it meets the requirements of the regular expression, return true // if it does not meet the requirements of the regular expression, return false}
CODE CASE
Next, write a method specifically used to verify the string format.
// Regex Regular Expression // the string to be verified by inputStr // helpText provides a span tag for information prompting // 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 whether the parameter is null {return false ;} return validateRegExp (/^ \ d {2} \/\ d {2} \/\ d {4} $/, inputFild, helpText, "Enter the correct date format"); // call the regular expression verification method}
Now, this article is over. Thank you for your support for the help House website!
Articles you may be interested in:
- Regular Expressions for JavaScript form verification [recommended]
- Sample Code for JavaScript Form Verification Using Regular Expressions
- Javascript Regular Expression Form Verification Code
- Regular Expression Form Verification Code commonly used in JavaScript
- Js uses regular expressions to verify the form (relatively complete resources)
- JavaScript uses regular expressions to Verify phone numbers.
- Verify the phone number using a regular expression in the JavaScript form
- Form Verification in Javascript-Regular Expressions first recognized