Date validation Regular expression

Source: Internet
Author: User
Tags regular expression

The code is as follows Copy Code

$arr _date = Array (
' 2004-06-01 ',
' 2005-0x-10 ',
' 12-12-12 ',
' 2000-12-25 00:10:20 ',
' 2007-12-05 '
);

for ($i =0; $i <5; + + $i)
{
$date = $arr _date[$i];
if (Ereg ([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}), $date, $regs))
{
echo "date string $date conforms to ' YYYY-MM-DD ' format:";
echo "$regs [1]. $regs [2]. $regs [3]<br/><br/>";
}
Else
{
echo "<b> date string $date does not conform to the ' YYYY-MM-DD ' format date string </b><br/><br/>";
}
}

Matching YYYY-MM-DD

The code is as follows Copy Code

([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[13578]|1[02])-(0[1-9]|[ 12][0-9]|3[01]) | ((0[469]|11)-(0[1-9]|[ 12][0-9]|30)) | (02-(0[1-9]|[ 1][0-9]|2[0-8])) | (([0-9]{2}) (0[48]|[ 2468][048]| [13579] [26]) | ((0[48]| [2468] [048]| [3579] [26]) 00))-02-29)

2: Matching dd/mm/yyyy or matching d/m/yyyy

The code is as follows Copy Code
'/^ ([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}) /(([13578]|1[2]) | (0[13578]|1[02]) '
.' /((0[1-9]| [12] [0-9]|3[01]) | ([1-9]| [12] [0-9]|3[1])) | ((0[469]|11) | ([469]|11)] '
.' /((0[1-9]| [12] [0-9]|30) | ([1-9]| [12] [0-9]|30)] | '
.' ((02|2)/((0[1-9]|[ 1][0-9]|2[0-8]) | ([1-9]| [1] [0-9]|2[0-8])) '
.')))| ((([0-9]{2}) (0[48]|[ 2468][048]| [13579] [26]) | ([48]| [2468] [048]| [13579] [26]) | '
.' ((((0[48]) | ([48]) | [2468] [048]| [3579] [26]) 00))/(02|2)/29) $/';

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.