Page 1 of 1

Methods for making sure date is entered in correct format

Posted: Thu Aug 05, 2004 6:02 am
by JayBird
A problem i always have is making sure people enter dates into forms int he correct format.

Say i wanted all dates to be ntered like 05-08-2004 i have a number of options

Option 1
Let the user enter anything they want, then use REGEX to check it. If it is wrong, spit out an error.

Good thing about this is that it is less time consuming for the user to fill in the dates as there are going to be quite a few date fields on the forms i am using

Option 2
Use select boxes in the form, so the user can only select options that are correct, not allowing them to enter incorect data. Making the user select the day number, the month number and the year from a drop down can be time consuming and laborious when there are a few date fields to be completed.

What other options could there be? What method would you go for?

Mark

Posted: Thu Aug 05, 2004 8:29 am
by Bill H
I use option 2 but I "preset" the dates whenever possible so that, more often than not,
all they have to change (if anything) is the day.

Maybe you can't always make the necessary assumptions, but...

Posted: Thu Aug 05, 2004 8:48 am
by CoderGoblin
1) if using a database you cast try to cast the date using a select and see if a result is returned.
2) Use the php strtotime function
http://www.php.net/manual/en/function.strtotime.php

Example of use ripped from the page...

Code: Select all

<?php
$str = 'Not Good';
if (($timestamp = strtotime($str)) === -1) {
   echo "The string ($str) is bogus";
} else {
   echo "$str == " . date('l dS of F Y h:i:s A', $timestamp);
}
?>
Advantage user can put in additional information such as

Code: Select all

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>

Posted: Thu Aug 05, 2004 11:21 am
by JayBird
CoderGoblin: That seems like a pretty good method. Im gonna implement it and see how it goes.

Cheerz

Mark

Posted: Thu Aug 05, 2004 4:13 pm
by pickle
I'd use the pulldown menus. It requires on less page load. Alternatively, you could pop up a calendar and let them just click . I use http://javascript.internet.com/calendar ... icker.html often