Re: checking user input of MM-DD-YYYY

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Tue, January 15, 2008 8:27 am, Adam Williams wrote:
> I'm having users enter dates in MM-DD-YYYY format.  is there a way to
> check if what they have entered is invalid (like if they enter
> 1-15-2008
> instead of 01-15-2008) ?

Making the user type the 0 is just plain rude. :-)

You could use a javascript popup calendar to make MOST users have it
the way you want.

And something like this can validate what you think you want:
if (preg_match('/^([0-9]{2}-[0-9]{2}-[0-9]{4}$/', $date, $date_parts)){
  list($month, $day, $year) = $date_parts[1];
  $unixdate = mktime(1, 0, 0, $month, $day, $year);
  $crosscheck = date('M-d-Y', $unixdate); //Is M for zero-padded month?
  if ($crosscheck != $date_parts[0]) error();
}
else error();

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux