Path: csiph.com!usenet.pasdenom.info!news.albasani.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: "M. Strobel" Newsgroups: comp.lang.php Subject: Re: Simpler way to validate form fields? Date: Thu, 06 Dec 2012 17:50:01 +0100 Lines: 53 Message-ID: References: <2lusb8ldn2g35j7p025r8ep45i5g1gkat2@4ax.com> <2m8ub8dm7cq38dbtorvlpbs4nq85h1q3gu@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 4MmDAXS9y3ZFUZVzlIrbHg/P6mV+n2oKGC01jONlTLUNQLc6FBRR8l99blgslJN6s= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121025 Thunderbird/16.0.2 In-Reply-To: Xref: csiph.com comp.lang.php:7733 Am 06.12.2012 14:55, schrieb Gilles: > On Thu, 06 Dec 2012 14:43:57 +0100, Gilles wrote: > >> function getDateFromForm($key, $l=8, $val=null) { >> //Check for date DD/MM/YYYY >> return (isset($_POST[$key])) ? >> filter_var(substr($_POST[$key],0,$l), >> >> FILTER_VALIDATE_REGEXP,array("options"=>array("regexp"=>"/^\d{2}\/\d{2}\/\d{4}/")) >> ) >> : $val; >> } > > I made a mistake, but even after turning $_POST into $_GET, it still > displays nothing: > > function getDateFromForm($key, $l=8, $val=null) { > //Check for date DD/MM/YYYY > return (isset($_GET[$key])) ? > filter_var(substr($_GET[$key],0,$l), > > FILTER_VALIDATE_REGEXP,array("options"=>array("regexp"=>"/^\d{2}\/\d{2}\/\d{4}/")) > ) > : $val; > } > > //http://192.168.0.1/test.php?mydate=01/02/2012 > print getDateFromForm("mydate"); I don't know offhand what exactly PHP makes of your GET URL, but be aware that the forward slash is a path separator. The regex is okay. Find ways to quickly test code bits, or functions, if possible just use the command line. My test (newlines inserted for reading): php > $d = "01/01/2112"; php > echo filter_var($d, FILTER_VALIDATE_REGEXP,array("options"=>array("regexp"=>"/^\d{2}\/\d{2}\/\d{4}/"))), PHP_EOL; 01/01/2112 php > echo urlencode($d), PHP_EOL; 01%2F01%2F2112 Write regexp here with a different separator, easier to read: array("regexp"=>"#^\d{2}/\d{2}/\d{4}#") /Str.