There is not much change, mainly because the error message is returned, so that the alert part can be called. It is said that regular expressions can be used for verification. We will study it again next time. At least the author and source should be indicated for the streamer! Http://www.cnblogs.com/GuominQiu
The Code is as follows:
//---------------------------------------------------------------------------
// Determine whether the date format is correct
// The returned value is an error message, indicating a valid date string without an error message
Function isDateString (strDate ){
Var strSeparator = "-"; // Date Separator
Var strDateArray;
Var intYear;
Var intMonth;
Var intDay;
Var boolLeapYear;
Var ErrorMsg = ""; // error message
StrDateArray = strDate. split (strSeparator );
// The length is not determined. In fact, is also reasonable // strDate. length! = 10 |
If (strDateArray. length! = 3 ){
ErrorMsg + = "the date format must be yyyy-MM-dd ";
Return ErrorMsg;
}
IntYear = parseInt (strDateArray [0], 10 );
IntMonth = parseInt (strDateArray [1], 10 );
IntDay = parseInt (strDateArray [2], 10 );
If (isNaN (intYear) | isNaN (intMonth) | isNaN (intDay )){
ErrorMsg + = "Date Format error: year, month, and day must be pure numbers ";
Return ErrorMsg;
}
If (intMonth> 12 | intMonth <1 ){
ErrorMsg + = "Date Format error: month must be between 1 and 12 ";
Return ErrorMsg;
}
If (intMonth = 1 | intMonth = 3 | intMonth = 5 | intMonth = 7
| IntMonth = 8 | intMonth = 10 | intMonth = 12)
& (IntDay> 31 | intDay <1 )){
ErrorMsg + = "Date Format error: the number of days in a large month must be between 1 and 31 ";
Return ErrorMsg;
}
If (intMonth = 4 | intMonth = 6 | intMonth = 9 | intMonth = 11)
& (IntDay> 30 | intDay <1 )){
ErrorMsg + = "Date Format error: the number of days in a small month must be between 1 and 31 ";
Return ErrorMsg;
}
If (intMonth = 2 ){
If (intDay <1 ){
ErrorMsg + = "Date Format error: date must be greater than or equal to 1 ";
Return ErrorMsg;
}
BoolLeapYear = false;
If (intYear % 100) = 0 ){
If (intYear % 400) = 0)
BoolLeapYear = true;
}
Else {
If (intYear % 4) = 0)
BoolLeapYear = true;
}
If (boolLeapYear ){
If (intDay> 29 ){
ErrorMsg + = "Date Format error: the number of days in March of a leap year cannot exceed 29 ";
Return ErrorMsg;
}
} Else {
If (intDay> 28 ){
ErrorMsg + = "Date Format error: the number of days in February of a non-leap year cannot exceed 28 ";
Return ErrorMsg;
}
}
}
Return ErrorMsg;
}