Regular Expression of yyyy-mm-dd

Source: Internet
Author: User

Let me modify a JavaScript method in the header today to verify that the input date meets the requirements. Well. Our requirements are in the format of yyyy-mm-dd, and none of the others meet the requirements. The original method does not use regular expressions and uses a bunch of judgments.

In fact, I also have a headache with regular expressions. Baidu has some materials. To sum up, leave a backup here.

The question to consider is: the valid date, the number of days per month, and the leap year ....

1. valid date: the date and time between midnight on January 1, January 1, 0001 and 12:00:00 on January 1, December 31, 9999 in msdn.

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

2. Concept of a leap year: Baidu encyclopedia-four years a week, a hundred years not enough, four hundred years again

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

3. Integrated expression

Normal year:

  • The year can be written in a unified manner :(?! (0000) [0-9] {4}
  • The month of all years, including the Year of the same year, includes the day 1-28: (0 [1-9] | 1 [0-2]) -(0 [1-9] | 1 [0-9] | 2 [0-8])
  • All years, including the Year of the same year, except the Year of the same month, include the 29 and 30 days: (0 [13-9] | 1 [0-2])-(29 | 30)
  • All years, including year 1, 3, 5, 7, 8, 10, and December, contain 31 days: (0 [13578] | 1 [02])-31)
  • All the other dates except the February 29 of the leap year :(?! 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)

Runnian

  • Year that can be divisible by 4 but cannot be divisible by 100: ([0-9] {2} (0 [48] | [2468] [048] | [13579] [26])
  • The year in which the object can be divisible by 400. The number that can be divisible by 400 is certainly 100, so the last two digits must be 00: (0 [48] | [2468] [048] | [13579] [26]) 00
  • The sum 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 the four rules have been implemented and have no influence on each other. The combination is the regular expression of all dates that conform to 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) $

Considering that this regular expression is only used for verification, it is meaningless to capture a group. It only occupies resources and affects matching efficiency. Therefore, you can use a non-capture 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 [1, 13578] | 1 [02])-31) | (? : [0-9] {2 }(? : 0 [48] | [2468] [048] | [13579] [26]) | (? : 0 [48] | [2468] [048] | [13579] [26]) 00)-02-29) $

For capture/non-capture of regular expressions, see: http://www.cnblogs.com/wuhong/archive/2011/02/18/1957017.html

The last is the updated JavaScript method.

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");
    } 

}
<Body>
<Input type = "date" name = "textfield" id = "text1">
<Input type = "button" value = "button" onclick = "checkdate ('text1')">
</Body>

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.