michaelmcgurk michaelmcgurk - 11 months ago 82
PHP Question

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

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 Source

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