Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> General/Other Stuff
Author Message
jaredg16
New Member
New Member


Joined: Jun 27, 2006
Posts: 15
Location: Baton Rouge, LA

PostPosted: Fri Jul 14, 2006 11:24 am Reply with quote

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
 
View user's profile Send private message AIM Address Yahoo Messenger
Raven
Site Admin/Owner


Joined: Aug 27, 2002
Posts: 17077

PostPosted: Fri Jul 14, 2006 2:54 pm Reply with quote

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.
 
View user's profile Send private message
jaredg16
PostPosted: Fri Jul 14, 2006 3:03 pm Reply with quote

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

PostPosted: Fri Jul 14, 2006 9:26 pm Reply with quote

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!! 
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
Raven
PostPosted: Fri Jul 14, 2006 11:50 pm Reply with quote

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
PostPosted: Sat Jul 15, 2006 9:28 am Reply with quote

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
PostPosted: Sat Jul 15, 2006 12:08 pm Reply with quote

Regular expressions (ereg). The ^ signifies the start of the pattern and the $ then end.
 
gregexp
PostPosted: Sat Jul 15, 2006 1:29 pm Reply with quote

Ahhh, I didnt read that on php.net, thank you for clearing that up.
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> General/Other Stuff

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©