Javascript: how to use javascript to determine whether the input date is legal
How to use javascript to determine whether the input date is valid
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) | (12 <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) | (31 <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 | 12 <nMonth)
{
Return false;
}
If (nDay <1 | 31 <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) | (31 <nDay ))
{
Return false;
}
}
Else
{
If (nDay <1) | (30 <nDay ))
{
Return false;
}
}
Return true;
}