How to use JavaScript to determine whether the date of entry is legal

Source: Internet
Author: User
Tags date
javascript| Date

How to use JavaScript to determine whether the date of entry is legal

function Isvalidyear (psyear)
{
var syear = new String (psyear);


if (psyear==null)
{
return false;
}


if (isNaN (psyear) ==true)
{
return false;
}

if (Syear = "")
{
return true;
}

if (Syear.match (/[^0-9]/g)!=null)
{
return false;
}

var nyear = parseint (Syear, 10);

if ((Nyear < 0) | | (9999 < nyear))
{
return false;
}

return true;
}


function Isvalidmonth (psmonth)
{
var smonth = new String (psmonth);

if (psmonth==null)
{
return false;
}

if (isNaN (psmonth) ==true)
{
return false;
}

if (Smonth = "")
{
return true;
}

if (Smonth.match (/[^0-9]/g)!=null)
{
return false;
}

var nmonth = parseint (smonth,10);

if ((Nmonth < 0) | | (< Nmonth))
{
return false;
}

return true;
}


function Isvalidday (psday)
{
var sday = new String (psday);

if (psday==null)
{
return false;
}

if (isNaN (psday) ==true)
{
return false;
}

if (Sday = "")
{
return true;
}

if (Sday.match (/[^0-9]/g)!=null)
{
return false;
}

var nday = parseint (Psday, 10);

if ((Nday < 0) | | (< Nday))
{
return false;
}

return true;
}


function Isvaliddate (psyear, Psmonth, Psday)
{
if (psyear==null | | psmonth==null | | psday==null)
{
return false;
}

var syear = new String (psyear);
var smonth = new String (psmonth);
var sday = new String (psday);

if (Isvalidyear (syear) ==false)
{
return false;
}

if (Isvalidmonth (smonth) ==false)
{
return false;
}

if (Isvalidday (sday) ==false)
{
return false;
}

var nyear = parseint (Syear, 10);
var nmonth = parseint (Smonth, 10);
var nday = parseint (Sday, 10);

if (syear== "" && smonth== "" && sday== "")
{
return true;
}

if (syear== "" | | smonth== "" | | sday== "")
{
return false;
}

if (Nmonth < 1 | | | < nmonth)
{
return false;
}
if (Nday < 1 | | < Nday)
{
return false;
}

if (Nmonth = 2)
{
if ((nyear% 400 = 0) | | (nyear% 4 = 0) && (nyear% 100!= 0))
{
if ((Nday < 1) | | (Nday > 29))
{
return false;
}
}
Else
{
if ((Nday < 1) | | (Nday > 28))
{
return false;
}
}
}
else if ((Nmonth = 1) | |
(Nmonth = 3) | |
(Nmonth = 5) | |
(Nmonth = 7) | |
(Nmonth = 8) | |
(Nmonth = 10) | |
(Nmonth = 12))
{
if ((Nday < 1) | | (< Nday))
{
return false;
}
}
Else
{
if ((Nday < 1) | | (< Nday))
{
return false;
}
}

return true;
}



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.