Regular Expression Help for Date Validation - dd/mm/yyyy - PHP

michaelmcgurk picture michaelmcgurk · Feb 15, 2012 · Viewed 16.6k times · Source

Can someone show me the error of my ways when it comes to this regular expression:

if(preg_match("/^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$/", $_POST["date"]) === 0) {
   echo 'error';
}

Basically I want this to display an error message each time - unless the format is correct (dd/mm/yyyy).

What am I doing wrong with the above?

Many thanks for any pointers.

-- updated regex above shortly after posting - apologies for inconvenience --

Answer

iDifferent picture iDifferent · Feb 15, 2012

I think you should escape the slashes /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/