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>