Using filter_var() to verify date?
filtering, php
Solution
$myregex = '~^\d{2}/\d{2}/\d{4}$~';
The regex matched because you just require that pattern anywhere in the string. What you want is only that pattern and nothing else. So add `^` and `$`.
Note that this still doesn't mean the value is a valid date. `99/99/9999` will pass that test. I'd use:
if (!DateTime::createFromFormat('d/m/Y', $string))
Problem
I'm obviously not using filter_var() correctly. I need to check that the user has entered a valid date, in the form "dd/mm/yyyy". This simply returns whatever I passed as a date, while I expected it to return either the date or 0/null/FALSE in case the input string doesn't look like a date: ``` $myregex = "/\d{2}\/\d{2}\/\d{4}/"; print filter_var("bad 01/02/2012 bad",FILTER_VALIDATE_REGEXP,array("options"=>array("regexp"=> $myregex))); ``` If someone else uses this function to check dates, what am I doing wrong? Should I use another function to validate form fields? Thank you.