Details about how to verify the time format of js regular expressions in the format of xxxx-xx,

Source: Internet
Author: User

Details about how to verify the time format of js regular expressions in the format of xxxx-xx,

In projects, we often encounter time problems. Sometimes the time plug-in is used to allow users to choose from, but sometimes the time can be input at customers' requirements, how can we determine whether the time format entered by the user is correct and whether the input time is valid?

In this case, you need to use a regular expression. Next, we will briefly describe my verification instance, and take the validation time format as an example:

1. First, we need to obtain the user input;

2. Secondly, we need to verify whether the content entered by the user is in format;

3. Verify that the entered time is valid after the format is correct;

4. If the time format entered by the user is correct and valid, the content in the input box is returned. Otherwise, the user is prompted to enter the time again.

The verification process is as follows:

1. Get user input time:

var bagin = $('.input_one').val();

2. Verify that the time format is correct: (the format of the timestamp is returned for verification, for example: (2017-01-01,7 7,-, 01,-, 01); otherwise, null is returned)

Var bagin_r = bagin. match (/^ (\ d {4}) (-) (\ d {2}) (-) (\ d {2}) $ /); if (bagin_r = null) {alert ("enter the correct start time format, for example, 2017-01-01"); return false ;}

3. Check whether the verification time is valid: (Note: This segment must be placed after the verification time format is complete)

Var B _d = new Date (bagin_r [1], bagin_r [3]-1, bagin_r [5]); var B _num = (B _d.getFullYear () = bagin_r [1] & (B _d.getMonth () + 1) = bagin_r [3] & B _d.getDate () = bagin_r [5]); if (B _num = 0) {alert ("the start time is invalid. Enter the correct Start Time"); return false ;}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.