JavaScript Regular expression Validation date (difference excepting and leap year)

Source: Internet
Author: User
Tags datetime

A DateTime value type represents a range of values in the year (Christian ERA), January 1, 01 12:00:00 to A.D. (C.E.) December 31, 9999 between 11:59:59.

Let's get to the point.
First you need to validate the year, and obviously the year range is 0001-9999, and the regular expression matching yyyy is:

[0-9] {3} [1-9]| [0-9] {2} [1-9] [0-9] {1}| [0-9] {1} [1-9] [0-9] {2}| [1-9] [0-9] {3}
[0-9] can also be expressed as/d, but/d is not as intuitive as [0-9], so I will always use [0-9]

The difficulty of validating a date with a regular expression is two: one is the number of days in the size of the month, and the other is the consideration of leap years.
For the first difficulty, we first do not consider leap years, assuming that February is 28 days, so that the month and date can be divided into three kinds of situations:

Only the regular of month and day is considered below

1. The month of all years, including excepting, contains 1-28 days

(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])

2. All years, including excepting, include 29 and 30th except February

(0[13-9]|1[0-2])-(29|30)

3. All years, including excepting, 1, 3, 5, 7, 8, 10, December all included 31st

(0[13578]|1[02])-31)

All other dates except February 29 in a 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)


Then we'll solve the second difficulty: Leap year considerations. Based on the definition of leap years, we can divide a leap year into two categories:

1 years that can be divisible by 4 but not divisible by 100. Looking for the change rule of the latter two digits, you can quickly get the following regular match:
([0-9]{2}) (0[48]| [2468] [048]| [13579] [26])
2, the 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, so we just have to ensure that the first two potentials are divisible by 4, and the corresponding regular expressions are:
(0[48]| [2468] [048]| [3579] [26]) 00
2. The strongest validation date of the regular expression, added a leap year validation

This date regular expression supports
Yyyy-mm-dd
Yyyy/mm/dd
Yyyy_mm_dd
YYYY. JMI The form of DD

match:2008-2-29 2008/02/29

Not match:2008-2-30 2007-2-29


1: Four Years a leap
([0-9]{2} (0[48]|[ 2468][048]| [13579] [26])

2: Hundred years does not leap, 400 year again leap
(0[48]| [2468] [048]| [13579] [26]) 00

3: 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) $

Sum up, we're writing a perfect date validation function.

The code is as follows Copy Code

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 the correct format is: 20040101");
return false;
}
var ss=strdate.split ("/");
var year=ss[0];
var month=ss[1];
var date=ss[2];
var year=strdate.substring (0,4);
var month=strdate.substring (4,6);
var date=strdate.substring (6,8);
alert (year+month+date);
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 entered incorrectly, please re-enter!");
return false;
}
else if (parseint) <1950 | | parseint (YEAR) >2050)
{
Alert ("Year should be between 1950-2050!");
return false;
}
else return true;
}
function Checkmonth (month) {
if (parseint (month,10)) {alert ("The month input is incorrect, please re-enter!"); return false; isNaN
else if (parseint (month,10) <1 | | parseint (month,10) >12)
{Alert ("month should be between 1-12!");
return false;}
else return true;
}
function Checkdate (year,month,date) {
var daysofmonth=caldays (parseint (year), parseint (month));
if (isNaN (parseint (date)) {alert ("Enter the date incorrectly, please re-enter!");
else if (parseint (date) <1| | parseint (date) >daysofmonth) {alert ("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 && year%100!=0) | | (year%400==0)) return true;
else return false;
}

Summarize

The year to be divisible by 400. The number divisible by 400 can surely be divisible by 100, so the latter two must be 00, so we just have to make sure that the first two are divisible by 4, and the corresponding regular expression is,
about the Gregorian leap year is the rule: the Earth's orbit around the sun is called a return years, a return to the older 365 days 5:48 46 seconds. Therefore, the Gregorian calendar has excepting and leap years, excepting 365 days a year, 0.2422 days shorter than the return, four years a total of 0.9688 days, so every four years to increase the day, this year has 366 days, is leap years. However, the average length of the Gregorian calendar year is approximately the same as that of the year of return, when the four increase is 0.0312 days more than the four years, and 3.12 days after 400 years, so that 400 years are set for 3 year years, which means only 400 leap years in the Year 97. This stipulates that the year is a multiplier of 400 for the whole hundred years, such as 1900, 2100, not leap years.

Related Article

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.