JavaScript Validate a Valid Date formatted as "MM/DD/YYYY"

Source: Internet
Author: User

Validates the input string is a valid date formatted as "MM/DD/YYYY"
function Isvaliddate (datestring) {
First check for the pattern
if (!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test (datestring)) {
return false;
}
Parse the date parts to integers
var parts = datestring.split ("/");
var day = parseint (parts[1], 10);
var month = parseint (Parts[0], 10);
var year = parseint (parts[2], 10);

Check the ranges of month and year
if (Year < + | | year > | | month = = 0 | | month > 12) {
return false;
}
var monthlength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

Adjust for leap years
If (year% 400 = = 0 | | (Year% + = 0 && Year% 4 = = 0))
MONTHLENGTH[1] = 29;

Check the range of the day
Return Day > 0 && Day <= monthlength[month-1];
};

JavaScript Validate a Valid Date formatted as "MM/DD/YYYY"

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.