Regular expression _ regular expression that matches the Yyyy-mm-dd date format

Source: Internet
Author: User
Tags datetime
In fact, I also have a headache regular expression, Baidu some information, summed up, here to leave a backup bar.

The question to consider: what is the legal date, the number of days per month is different, the question of a leap year ....

1. Legal Date: MSDN Rules--the date and time between 12:00:00 midnight, January 1, 01 (C.E.) December 31, 9999, and 11:59:59 in A.D. (Christian ERA)

View Http://msdn.microsoft.com/zh-cn/library/system.datetime (vs.80). aspx

2, the concept of a leap year: Baidu Encyclopedia description-four years a leap, a century does not leap, 400 years before the leap

View Http://baike.baidu.com/view/29649.htm

3, the integration of expression

Excepting

Year can be unified writing: (?!) 0000) [0-9]{4}
The month of all years including excepting includes 1-28 days: (0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])
All years including excepting included 29 and 30th in addition to February: (0[13-9]|1[0-2)-(29|30)
All years including excepting 1, 3, 5, 7, 8, 10, December all contain 31st: (0[13578]|1[02])-31)
All other dates apart from the leap year of February 29 are: (?! 0000) [0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8]) | ( 0[13-9]|1[0-2])-(29|30) | (0[13578]|1[02])-31)
Run year

Year that can be divisible by 4 but not divisible by 100: ([0-9]{2} (0[48]|[ 2468][048]| [13579] [26])
Year that can be divisible by 400. The number divisible by 400 can certainly be divisible by 100, so the latter two must be 00: (0[48]|[ 2468][048]| [13579] [26]) 00
Together is the February 29 of all Leap years: ([0-9]{2} (0[48]|[ 2468][048]| [13579] [26]) | (0[48]| [2468] [048]| [13579] [26]) 00)-02-29)
All four rules are implemented and have no effect on each other, and together is the regular of all dates that match the DateTime range
^((?! 0000) [0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8]) | ( 0[13-9]|1[0-2])-(29|30) | (0[13578]|1[02])-31 | ([0-9]{2} (0[48]|[ 2468][048]| [13579] [26]) | (0[48]| [2468] [048]| [13579] [26]) 00)-02-29) $

Given that this regular expression is used only as validation, capturing groups are meaningless, consume resources and affect the efficiency of matching, so you can use a non-capturing group for optimization.
^(?:(?! 0000) [0-9]{4}-(?:(?: 0 [1-9]|1[0-2])-(?: 0 [1-9]|1[0-9]|2[0-8]) | (?: 0 [13-9]|1[0-2])-(?: 29|30) | (?: 0 [13578]|1[02])-31 | (?: [0-9]{2} (?: 0 [48]| [2468] [048]| [13579] [26]) | (?: 0 [48]| [2468] [048]| [13579] [26]) 00)-02-29) $

Capture/non-capture of regular expressions see: http://www.jb51.net/article/28035.htm

Finally, the updated JavaScript method

Copy Code code as follows:

function Checkdate (fname) {
var sc = $ ("#" +fname);
var s = sc.val ();
if (sc==null) {
Alert ("Element is null");
return true;
}
var reg=/^ (?:(?! 0000) [0-9]{4}-(?:(?: 0 [1-9]|1[0-2])-(?: 0 [1-9]|1[0-9]|2[0-8]) | (?: 0 [13-9]|1[0-2])-(?: 29|30) | (?: 0 [13578]|1[02])-31 | (?: [0-9]{2} (?: 0 [48]| [2468] [048]| [13579] [26]) | (?: 0 [48]| [2468] [048]| [13579] [26]) 00)-02-29 $/;
if (!s.match (reg)) {
Alert ("false");
}else{
Alert ("true"); }

}

Copy Code code as follows:

<body>
<input type= "Date" name= "TextField" id= "Text1" >
<input type= "button" value= buttons "onclick=" checkdate (' Text1 ') ">
</body>

correct regular expressions (including test code):
Copy Code code as follows:

var str = ' 2009-12-33 ';
if (Str.match (/^: 19|20) \d\d)-(0[1-9]|1[012))-(0[1-9]|[ 12][0-9]|3[01]) ($/)) {
Alert (' is Date ');
} else {
Alert (' not date ');
}

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.