Form validation for JavaScript-initial recognition of regular expression _javascript techniques

Source: Internet
Author: User
Tags html form

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!

Related Article

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.