Re: complex if statement for field validation

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

 



David Mehler wrote:
> Hello,
> I've got a form with three fields that are not required for proper
> completion of it, ending month, day, year fields. If a user enters
> nothing no problem, but if those form fields are entered I need them
> validated. They have to be in the correct format YYYY-MM-DD date
> format and that value also has to be greater than the starting date
> validated previously. I've got some not working code.
> Thanks.
> Dave.
> 
> // if an ending date field was entered validate that
> // also must be greater than the starting date values
> if(!empty($_POST['month1']) && !empty($_POST['day1']) &&
> !empty($_POST['year1']) {
> $month1=$_POST['month1'];
> $day1=$_POST['day1'];
> $year1=$_POST['year1'];
> $date_value1="$year1-$month1-$day1";
> }
> if(!checkdate($month1,$day1,$year1)) {
> echo "Invalid Date.\n";
> } else {
> echo "Entered Date is correct.\n";
> }
> }
> if(!checkdate($month1,$day1,$year1)) > $date_value {
> echo "Invalid Date.\n";
> } else {
> echo "Entered Date is correct.\n";
> }
> 

Well, I don't know if they are cut/paste errors, but you have a few syntax
errors in the above code...

# Initialize your date container variables

$date_value_ts = $date_value1_ts = null;


# Setup start date stuff
if (  !empty($_POST['month']) &&
      !empty($_POST['day']) &&
      !empty($_POST['year']) )
{
	$month		= (int)$_POST['month'];
	$day		= (int)$_POST['day'];
	$year		= (int)$_POST['year'];
	$date_value	= "{$year}-{$month}-{$day}";


	#if ( !checkdate($month, $day, $year) )
	if ( ($date_value_ts = strtotime($date_value) ) === FALSE )
	{
		echo "Invalid Date.\n";
	} else {
		echo "Entered Date is correct.\n";
	}

}



Test for
if (  !empty($_POST['month1']) &&
      !empty($_POST['day1']) &&
      !empty($_POST['year1']) )
{
	$month1	= (int)$_POST['month1'];
	$day1	= (int)$_POST['day1'];
	$year1	= (int)$_POST['year1'];
	$date_value1= "{$year1}-{$month1}-{$day1}";

	#if ( !checkdate($month1, $day1, $year1) )
	if ( ($date_value1_ts = strtotime($date_value1) ) === FALSE )
	{
		echo "Invalid Date.\n";
	} else {
		echo "Entered Date is correct.\n";
	}

}

# Compare the two dates.  Make sure end date is after start date

if ( !is_null($date_value1_ts) &&
     ( (int)$date_value_ts < (int)$date_value1_ts ) )
{
	echo "Invalid Date.\n";
} else {
	echo "Entered Date is correct.\n";
}


-- 
Jim Lucas

A: Maybe because some people are too annoyed by top-posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

-- 
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