The regular expression child for JS Date Format validation. Simple writing should be
VaR Reg =/^ (/d {4}) ([/]) (/d {2}) ([/]) (/d {2}) $ /;
If (! Reg. Test (strdate )){
Alert ("the date format is incorrect! /N format: 2004/01/01 ");
Return false;
}
A while ago, the tester found that my original regular expression Sub-var Reg =/^ (/d {4}) ([/]) (/d {2}) ([/]) (/d {2})/; why does the validation for 2008/03/25 TTT fail. Later I found that there was a problem with the regular expression subexpression, And the symbol $ was missing at the end. The correct method was var Reg =/^ (/d {4}) ([/]). (/d {2}) ([/]) (/d {2}) $/
A complete JS Verification Method for yyyy/mm/DD.
<Script language = "javascript1.2">
Function checkdate (strdate ){
// Var strdate = Document. getelementbyid ("date_hour"). value;
VaR Reg =/^ (/d {4}) ([/]) (/d {2}) ([/]) (/d {2}) $ /;
If (! Reg. Test (strdate )){
Alert ("the date format is incorrect! /N format: 2004/01/01 ");
Return false;
}
VaR Ss = strdate. Split ("/");
VaR year = ss [0];
VaR month = ss [1];
VaR date = ss [2];
If (! Checkyear (year) {return false ;}
If (! Checkmonth (month) {return false ;}
If (! Checkdate (year, month, date) {return false ;}
Return true;
}
Function checkyear (year ){
If (isnan (parseint (year )))
{Alert ("the year is incorrect. Please enter it again! ");
Return false;
}
Else if (parseint (year) <1950 | parseint (year)> 2050)
{
Alert ("the year should be! ");
Return false;
}
Else return true;
}
Function checkmonth (month ){
If (isnan (parseint (month, 10) {alert ("Incorrect month input. Please enter it again! "); Return false ;}
Else if (parseint (month, 10) <1 | parseint (month, 10)> 12)
{Alert ("the month should be between 1 and 12! ");
Return false ;}
Else return true;
}
Function checkdate (year, month, date ){
VaR daysofmonth = caldays (parseint (year), parseint (month ));
If (isnan (parseint (date) {alert ("incorrect date input. Please enter it again! "); Return false ;}
Else if (parseint (date) <1 | parseint (date)> daysofmonth) {alert ("the date should be between 1-" + daysofmonth +! "); Return false ;}
Else return true;
}
Function caldays (year, month ){
VaR date = new date (year, month, 0 );
Return date. getdate ();
}
Function isleapyear (year ){
If (Year % 4 = 0 & amp; Year % 100! = 0) | (Year % 400 = 0) return true;
Else return false;
}
Function submitform (){
If ($ ('date _ hour ')){
$ ('Date _ hour'). value = '';
}
If ($ ('fromdate _ Day ')){
$ ('Fromdate _ Day'). value = '';
}
If ($ ('todate _ Day ')){
$ ('Todate _ Day'). value = '';
}
$ ('Logincountstatform'). Submit ();
}
</SCRIPT>