Re: checking user input of MM-DD-YYYY

From: Date: Tue, 15 Jan 2008 16:44:13 +0000
Subject: Re: checking user input of MM-DD-YYYY
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-267311@lists.php.net to get a copy of this message
Adam Williams wrote: > Thanks, I think I have it: > > $dateexplode = explode("-", $_POST["date_entered"]); > if (!preg_match("/^(\d{2})$/", $dateexplode[0],$data1) || > !preg_match("/^(\d{2})$/", $dateexplode[1],$data2) || > !preg_match("/^(\d{4})$/", $dateexplode[2],$data3)) > { > die ("you have entered an invalid date"); > } > > so if the person enters 01-15-2008 its fine, but 1-15-2008 dies. Running three regexes is a bit much when one is enough: /^([0-9]{2})-([0-9]{2})-([0-9]{4})$/ /Per Jessen, Zürich

« previous php.general (#267311) next »