Yes, you can use Regular expressions to handle date format input, but that is like using a hammer to squash a bug ;-). PHP has a great little function called sscanf (
) which parses an input string according to a format, essentially doing the reverse of sprintf.
Sscanf has the added value of not only checking for correct format of your input string, but returning an array of values based on what you want to parse.
Here is a test script to illustrate:
Code:
<html>
<body>
<form method="POST">
<input type=text name=inputdate>
<input type=submit>
</form>
<?php
if($_POST){
echo $inputdate . "<br><br>\n\n";
//get the values as an array
$date_in = sscanf($_POST["inputdate"], "%02d/%02d/%04d");
var_dump($date_in); // examine the array
//nice trick--dereference array as it is being created
list($month, $day, $year) = sscanf($_POST["inputdate"], "%02d/%02d/%04d");
echo "<br><br>\n\n
month = $month <br>
day = $day <br>
year = $year
";
}
?>
</body>
</html>
Just run this PHP script, enter a date in the form, and see how it validates. The format for sscanf used here is %02d for the two short numbers and %04d for the number with (you guessed it) 4 digits, such as the year.
Now, after you receive these values from the input string, you can easily use more PHP programming to check for other conditions you might want to check for in each of the returned values, such as
or some such.
It takes a little getting used to but once you learn the sscanf and sprintf rules, you are borrowing heavily from the C programming book, and your geek buddies might even start calling you a guru
![[thumbsup2] [thumbsup2] [thumbsup2]](/data/assets/smilies/thumbsup2.gif)
. -------------------------------------------
Big Brother: "War is Peace" -- Big Business: "Trust is Suspicion"
(