JavaScript validates the legality of a given date

Source: Internet
Author: User
Tags date format requires return

<script language= "JavaScript" >

Verify that the given date is valid and that the parameter format requires: YYYY-MM-DD can correct the expression according to the situation
function IsDate (ostartdate)
{
Validation of date formats requires a 2000-2,099 format of YYYY-MM-DD and can be converted to the correct date correctly
var pat_hd=/^20\d{2}-((0[1-9]{1}) | ( 1[0-2]{1})-((0[1-9]{1}) | ( [1-2] {1} [0-9] {1}) | (3[0-1]{1})) $/;

try{
The IF (!pat_hd.test (ostartdate)) {Throw date is illegal! ";}
var arr_hd=ostartdate.split ("-");
var datetmp;
datetmp= New Date (Arr_hd[0],parsefloat (arr_hd[1)) -1,parsefloat (arr_hd[2));
if (Datetmp.getfullyear ()!=parsefloat (arr_hd[0)) | | Datetmp.getmonth ()!=parsefloat (arr_hd[1])-1 | | dateTmp.getDate ( )!=parsefloat (arr_hd[2])
{
Throw "Date illegal! ";
}
}
catch (ex)
{
if (ex.description)
{return false;}
Else
{return false;}
}
return true;
}

Call

Alert (IsDate ("2005-12-12"));
Alert (IsDate ("2004-13-30"));
Alert (IsDate ("2005-12-32"));
Alert (IsDate ("2005-02-30"));

</script>



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.