Why Form Validation
1. To reduce the pressure on the server 2. The data input to ensure compliance
Common form validation
- Date Format
- Whether the form element is empty
- user name and password
- E-mail address
- identification number
Form selector
Attribute filter selector
Verify the contents of the form:
Use String object verification email: can not be empty, in the correct format.
Verification is not empty:
Search string: indexOf ():
Find the location of a specified string value of the first occurrence of a string
Verify the contents of the text box:
The password can not be empty, no less than six characters, the name can not be empty, we can not have a number.
Length validation:
Determining whether the digital string:
Using a for loop and substring () method of sequentially cut a single character, and then determines whether each character is a number
Form validation events, and methods
Form validation events, and methods requires a combination of elements:
Regular Expressions:
Why do we need a regular expression:
- Clean code
- Verify the contents of the text box example rigorous:
Example:
-
Normal mode:
var reg = / expression / additional parameters
-
[Expression must be a constant string]
[The use of certain additional parameters to represent rules: g represents a global matching may be performed; I representatives case insensitive; m may be representative of a plurality of rows match]
Constructor: var reg = new RegExp ( "expression", "additional parameters")
-
Simple mode:
We can only express specific match
-
Composite mode: You can use wildcard characters to express more abstract mode of rule
-
Use HTML5 way authentication form:
HTML5 New Verify attributes:
-
Example:
-