Forum Moderators: coopster

Message Too Old, No Replies

help with data validation

         

abliss26

3:27 pm on Apr 12, 2006 (gmt 0)

10+ Year Member



hi everyone, I'm pretty new to php, and need some help with validating text passed to a php scrpt $id = $_POST["mydate"]; the text is submitted from an html format, and should only have the format of yyyy-mm-dd ; I'm sure you seasoned programmers know how to check this rather efficiently; thanks very much.

dreamcatcher

3:38 pm on Apr 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not have drop down menu`s for date, month & year?

You can then pop the variables together.

$date = $__POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];

dc

abliss26

3:58 pm on Apr 12, 2006 (gmt 0)

10+ Year Member



This would limit form options, but I would think that I would still want to validate this data in the php script?

coopster

4:26 pm on Apr 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



True, you would always want to validate on the server-side.
$mydate = (isset($_POST['mydate'])) ? trim($_POST['mydate']) : ''; 
// Make sure the supplied date is in a yyyy-mm-dd format; if so, then
// make sure the supplied date is a valid date. If either of the
// edits is not TRUE, we have an invalid date, handle accordingly:
if (!preg_match [php.net]("/^(\d{4})-(\d{2})-(\d{2})$/", $mydate, $date) ¦¦
!checkdate [php.net]($date[2], $date[3], $date[1])) {
// Bad date!
// Or, you could initialize to a good date:
// $mydate = date [php.net]('Y-m-d');
}