Recommended reading: JavaScript form verification length
JavaScript Form validation-Submitting a form
JavaScript form Validation-uncovering the veil of the regular expression
JavaScript can be used to validate these input data in an HTML form before the data is sent to the server.
Validation date
The format of the date is relatively special and cannot be validated by its length
Common date formats: first two digits for the day, two digits for the month, and a four-digit year, with a slash-delimited period.
Mm/dd/yyyy
Blogger: How do you design a code that verifies that the date is in line with the format?
Let's take a look at the logic of validating date formats.
Remove the values from the form field and divide the string by "/" as an array
Parse the "month" substring, whether it is only two characters, and is a numeric
Parse "Day" substring, is only two characters, and all are numeric
Parse "Year" substring, whether it is only four characters, and is all numeric
Meet the above conditions, but also to determine whether the data entered only two diagonal, if more than two slash, after the content to ignore
Blogger: Don't be afraid, JavaScript it's quite powerful, it gives developers a powerful built-in tool--regular expressions
It is specifically used to match text patterns
It's like a policeman with a shiny eye, always observing the suspect's every move, and if it breaks the law, ╮(╯▽╰)╭ the bad guys out.
Now let's look at a small example
Match pattern =/^\d{5}$/
The character after the equals number is the next expression
First Slash and last slash: "/" regular expressions are surrounded by slashes
The second character: the "^" character must start with the specified mode, and the number cannot be used
Third character: "\d" represents a number
Fourth character: "{5}" the only number must repeat 5 times
Fifth character: "$" string ends in specified pattern
All right, form validation for JavaScript-the knowledge of the first regular expressions is introduced so much, the next section will uncover the regular expression of the veil. More content please pay attention to cloud Habitat community website!