Date Format JS verification yyyy/mm/dd

Source: Internet
Author: User
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>

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.