michaelmcgurk michaelmcgurk - 10 days ago 6
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

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

Comments