Author |
Message |
jaredg16
New Member


Joined: Jun 27, 2006
Posts: 15
Location: Baton Rouge, LA
|
Posted:
Fri Jul 14, 2006 11:24 am |
|
Okay so I have one field in which users enter their birthdate. I'm tyring to make a field validation that ensures they enter the date in the following format -- MM-DD-YYYY
I am working on an IF statement that checks the field (named "custom") to see if the data entered is in the correct format but I simply do not know what I should be checking.
Any suggestions???
Jared |
|
|
|
 |
Raven
Site Admin/Owner

Joined: Aug 27, 2002
Posts: 17088
|
Posted:
Fri Jul 14, 2006 2:54 pm |
|
I'm assuming you're asking how your form data gets passed to your PHP program.
Assume you have a form with a date field:
<form method="post" action="chk_date.php">
Please enter your birthdate (MM-DD-YYYY): <input type="text" name="bdate" value="MM-DD-YYYY" />
</form>
Your form will pass the "bdate" value to your "chk_date" script. You would use something like this to check it.
<?
$bdate = '';
$bdate = $_POST['bdate'];
//
// Cleansing code for $bdate
//
// begin pseudo logic:
if ($bdate_is_not_in_the_right_format) {
echo ERROR_MESSAGE;
return to script;
die();
// end pseudo logic
}
// Rest of program goes here. |
|
|
|
 |
jaredg16

|
Posted:
Fri Jul 14, 2006 3:03 pm |
|
Actually... my question is regarding the part of the IF statement that's bolded below. how do I validation that the entered data is in the correct format (MM-DD-YYYY)?
Quote: | // begin pseudo logic:
if ($bdate_is_not_in_the_right_format) {
echo ERROR_MESSAGE;
return to script;
die();
// end pseudo logic |
|
|
|
|
 |
gregexp
The Mouse Is Extension Of Arm

Joined: Feb 21, 2006
Posts: 1497
Location: In front of a screen....HELP! lol
|
Posted:
Fri Jul 14, 2006 9:26 pm |
|
What about
if(!date($bdate)){
die(" ERROR_MESSAGE");
}else{
continue............ |
_________________ For those who stand shall NEVER fall and those who fall shall RISE once more!! |
|
 |
 |
Raven

|
Posted:
Fri Jul 14, 2006 11:50 pm |
|
darklord, unfortunately that will not work.
I would recommend this code. Try different combinations for $bdate.
Code:
<?
$bdate = '07-15-2006';
$isValid = false;
// The 'if' statement tests for validity of NN-NN-NNNN only
if (ereg('^([0-9]{2})[-]([0-9]{2})[-]([0-9]{4})$', $bdate)) {
$dateParts = explode('-',$bdate); // Break $bdate into its parts
// checkdate validates that the parts (m,d,y) contain valid ranges
if (checkdate($dateParts[0],$dateParts[1],$dateParts[2])) {
$isValid = true;;
}
}
if (!$isValid) {
die('Invalid Date Format');
}
// Date is valid - Do other logic
echo 'Valid Date Format';
?>
|
|
|
|
|
 |
gregexp

|
Posted:
Sat Jul 15, 2006 9:28 am |
|
Whoa, I need to really read up on how to check validation, but thank you raven for the insight, I completly undertand.
except 2 things, whats the ^ for and the $ for?
Thats all Id like to know thanx. |
|
|
|
 |
Raven

|
Posted:
Sat Jul 15, 2006 12:08 pm |
|
Regular expressions (ereg). The ^ signifies the start of the pattern and the $ then end. |
|
|
|
 |
gregexp

|
Posted:
Sat Jul 15, 2006 1:29 pm |
|
Ahhh, I didnt read that on php.net, thank you for clearing that up. |
|
|
|
 |
|