Age Verification Based on DOB entered by user

Need error returned when anyone 75 or over tries to register.

Page 1 of 1

4 Replies - 3975 Views - Last Post: 29 July 2008 - 02:17 PM Rate Topic: -----

#1 gooroo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-July 08

Age Verification Based on DOB entered by user

Posted 02 July 2008 - 03:09 AM

Please help! I'm trying to validate user age based on the DOB entered. If the user if 75 or over, an error message is generated. Currently, user is able to enter their DOB but if they enter July 1, 1933 (which would make them 75), no error is generated.

PHP CODE

	$today = getdate();
	$today = $today['mon']."/".$today['mday']."/".$today['year'];
	$vars['m_dob'] = $vars['mdate_month']."/".$vars['mdate_day']."/".$vars['mdate_year'];

	$diff = (dateDiff( "/", $vars['m_dob'], $today) / 365 );
		//if( $diff < 0  )
//	$errors[] = "You entered an invalid date. Please review and correct."; else 
	if( $diff > 75 )
	$errors[] = "Sorry. Our Membership program age limit is 75.";



HTML CODE

<tr> 
	<th><label for="dob">Date of Birth</label>*<br />
			<label class="small">Members must be under 75 years of age.</label></th>
	  <td>{html_select_date prefix='mdate_' field_array='mdate' month_format='%m' month_empty='MM' day_empty='DD' year_empty='YYYY' start_year='-75' reverse_years='true'}
		</td>
	</tr>



SMARTY PLUGIN - HTML_SELECT_DATE

<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty {html_select_date} plugin
 *
 * Type:	 function<br />
 * Name:	 html_select_date<br />
 * Purpose:  Prints the dropdowns for date selection.
 *
 * ChangeLog:<br />
 *		   - 1.0 initial release
 *		   - 1.1 added support for +/- N syntax for begin
 *				and end year values. (Monte)
 *		   - 1.2 added support for yyyy-mm-dd syntax for
 *				time value. (Jan Rosier)
 *		   - 1.3 added support for choosing format for
 *				month values (Gary Loescher)
 *		   - 1.3.1 added support for choosing format for
 *				day values (Marcus Bointon)
 * @link [url=http://smarty.php.net/manual/en/language.function.html.select.date.php]http://smarty.php.net/manual/en/language.f...select.date.php[/url] {html_select_date}
 *	  (Smarty online manual)
 * @version 1.3.1
 * @author   Andrei Zmievski
 * @param array
 * @param Smarty
 * @return string
 */
function smarty_function_html_select_date($params, &$smarty)
{
	require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
	require_once $smarty->_get_plugin_filepath('function','html_options');
	/* Default values. */
	$prefix		  = "Date_";
	$start_year	  = strftime("%Y");
	$end_year		= $start_year;
	$display_days	= true;
	$display_months  = true;
	$display_years   = true;
	$month_format	= "%B";
	/* Write months as numbers by default  GL */
	$month_value_format = "%m";
	$day_format	  = "%02d";
	/* Write day values using this format MB */
	$day_value_format = "%d";
	$year_as_text	= false;
	/* Display years in reverse order? Ie. 2000,1999,.... */
	$reverse_years   = false;
	/* Should the select boxes be part of an array when returned from PHP?
	   e.g. setting it to "birthday", would create "birthday[Day]",
	   "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
	$field_array	 = null;
	/* <select size>'s of the different <select> tags.
	   If not set, uses default dropdown. */
	$day_size		= null;
	$month_size	  = null;
	$year_size	   = null;
	/* Unparsed attributes common to *ALL* the <select>/<input> tags.
	   An example might be in the template: all_extra ='class ="foo"'. */
	$all_extra	   = null;
	/* Separate attributes for the tags. */
	$day_extra	   = null;
	$month_extra	 = null;
	$year_extra	  = null;
	/* Order in which to display the fields.
	   "D" -> day, "M" -> month, "Y" -> year. */
	$field_order	 = 'MDY';
	/* String printed between the different fields. */
	$field_separator = "\n";
	$time = time();
	$all_empty	   = null;
	$day_empty	   = null;
	$month_empty	 = null;
	$year_empty	  = null;

	foreach ($params as $_key=>$_value) {
		switch ($_key) {
			case 'prefix':
			case 'time':
			case 'start_year':
			case 'end_year':
			case 'month_format':
			case 'day_format':
			case 'day_value_format':
			case 'field_array':
			case 'day_size':
			case 'month_size':
			case 'year_size':
			case 'all_extra':
			case 'day_extra':
			case 'month_extra':
			case 'year_extra':
			case 'field_order':
			case 'field_separator':
			case 'month_value_format':
			case 'month_empty':
			case 'day_empty':
			case 'year_empty':
				$$_key = (string)$_value;
				break;

			case 'all_empty':
				$$_key = (string)$_value;
				$day_empty = $month_empty = $year_empty = $all_empty;
				break;

			case 'display_days':
			case 'display_months':
			case 'display_years':
			case 'year_as_text':
			case 'reverse_years':
				$$_key = (bool)$_value;
				break;

			default:
				$smarty->trigger_error("[html_select_date] unknown parameter $_key", E_USER_WARNING);

		}
	}

	// If $time is not in format yyyy-mm-dd
	if (!preg_match('/^\d{0,4}-\d{0,2}-\d{0,2}$/', $time)) {
		// then $time is empty or unix timestamp or mysql timestamp
		// using smarty_make_timestamp to get an unix timestamp and
		// strftime to make yyyy-mm-dd
		$time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
	}
	// Now split this in pieces, which later can be used to set the select
	$time = explode("-", $time);

	// make syntax "+N" or "-N" work with start_year and end_year
	if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
		if ($match[1] == '+') {
			$end_year = strftime('%Y') + $match[2];
		} else {
			$end_year = strftime('%Y') - $match[2];
		}
	}
	if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
		if ($match[1] == '+') {
			$start_year = strftime('%Y') + $match[2];
		} else {
			$start_year = strftime('%Y') - $match[2];
		}
	}

	$field_order = strtoupper($field_order);

	$html_result = $month_result = $day_result = $year_result = "";

	if ($display_months) {
		$month_names = array();
		$month_values = array();
		if(isset($month_empty)) {
			$month_names[''] = $month_empty;
			$month_values[''] = '';
		}
		for ($i = 1; $i <= 12; $i++) {
			$month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
			$month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
		}

		$month_result .= '<select name=';
		if (null !== $field_array){
			$month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
		} else {
			$month_result .= '"' . $prefix . 'Month"';
		}
		if (null !== $month_size){
			$month_result .= ' size="' . $month_size . '"';
		}
		if (null !== $month_extra){
			$month_result .= ' ' . $month_extra;
		}
		if (null !== $all_extra){
			$month_result .= ' ' . $all_extra;
		}
		$month_result .= '>'."\n";

		$month_result .= smarty_function_html_options(array('output'	 => $month_names,
															'values'	 => $month_values,
															'selected'   => $month_values[(int)$time[1]],
															'print_result' => false),
													  $smarty);

		$month_result .= '</select>';
	}

	if ($display_days) {
		$days = array();
		if (isset($day_empty)) {
			$days[''] = $day_empty;
			$day_values[''] = '';
		}
		for ($i = 1; $i <= 31; $i++) {
			$days[] = sprintf($day_format, $i);
			$day_values[] = sprintf($day_value_format, $i);
		}

		$day_result .= '<select name=';
		if (null !== $field_array){
			$day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
		} else {
			$day_result .= '"' . $prefix . 'Day"';
		}
		if (null !== $day_size){
			$day_result .= ' size="' . $day_size . '"';
		}
		if (null !== $all_extra){
			$day_result .= ' ' . $all_extra;
		}
		if (null !== $day_extra){
			$day_result .= ' ' . $day_extra;
		}
		$day_result .= '>'."\n";
		$day_result .= smarty_function_html_options(array('output'	 => $days,
														  'values'	 => $day_values,
														  'selected'   => $time[2],
														  'print_result' => false),
													$smarty);
		$day_result .= '</select>';
	}

	if ($display_years) {
		if (null !== $field_array){
			$year_name = $field_array . '[' . $prefix . 'Year]';
		} else {
			$year_name = $prefix . 'Year';
		}
		if ($year_as_text) {
			$year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
			if (null !== $all_extra){
				$year_result .= ' ' . $all_extra;
			}
			if (null !== $year_extra){
				$year_result .= ' ' . $year_extra;
			}
			$year_result .= '>';
		} else {
			// $years = range((int)$start_year, (int)$end_year);
			$years = array();
			for ($i=$start_year;$i<=$end_year;$i++)
				$years[] = $i;
			if ($reverse_years) {
				rsort($years, SORT_NUMERIC);
			}
			$yearvals = $years;
			if(isset($year_empty)) {
				array_unshift($years, $year_empty);
				array_unshift($yearvals, '');
			}
			$year_result .= '<select name="' . $year_name . '"';
			if (null !== $year_size){
				$year_result .= ' size="' . $year_size . '"';
			}
			if (null !== $all_extra){
				$year_result .= ' ' . $all_extra;
			}
			if (null !== $year_extra){
				$year_result .= ' ' . $year_extra;
			}
			$year_result .= '>'."\n";
			$year_result .= smarty_function_html_options(array('output' => $years,
															   'values' => $yearvals,
															   'selected'   => $time[0],
															   'print_result' => false),
														 $smarty);
			$year_result .= '</select>';
		}
	}

	// Loop thru the field_order field
	for ($i = 0; $i <= 2; $i++){
		$c = substr($field_order, $i, 1);
		switch ($c){
			case 'D':
				$html_result .= $day_result;
				break;

			case 'M':
				$html_result .= $month_result;
				break;

			case 'Y':
				$html_result .= $year_result;
				break;
		}
		// Add the field seperator
		if($i != 2) {
			$html_result .= $field_separator;
		}
	}

	return $html_result;
}

/* vim: set expandtab: */

?>


:crazy:

** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Age Verification Based on DOB entered by user

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,880
  • Joined: 10-May 07

Re: Age Verification Based on DOB entered by user

Posted 02 July 2008 - 03:13 AM

Why are you setting errors with brackets?

$errors = "Sorry. Our Membership program age limit is 75.";



Once the error is set, I don't see where you reference the variable... why are you setting it?
Was This Post Helpful? 0
  • +
  • -

#3 gooroo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-July 08

Re: Age Verification Based on DOB entered by user

Posted 02 July 2008 - 03:57 AM

Not sure I know what you mean. I've pasted the entire PHP code, so hopefully it clarifies. I tried removing the brackets (like the code you sent) and still did not receive an error. Thank you.

<?php
  
function dateDiff($dformat, $endDate, $beginDate)
{
	$date_parts1=explode($dformat, $beginDate);
	$date_parts2=explode($dformat, $endDate);
	$start_date= getdate();
	$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
	
	return $end_date - $start_date;
} 

function vsf( &$vars ) {
	$errors = array(); 

/* Membership Start Date */
	$today = getdate();
	$today = $today['mon']."/".$today['mday']."/".$today['year'];
	$vars['m_dob'] = $vars['mdate_month']."/".$vars['mdate_day']."/".$vars['mdate_year'];

	$diff = (dateDiff( "/", $vars['m_dob'], $today) / 365 );
		//if( $diff < 0  )
//	$errors[] = "You entered an invalid date. Please review and correct."; else 
	if( $diff > 75 )
	$errors[] = "Sorry. Our Membership program age limit is 75.";
 	
	 //Email Match 
//		
//	if(!( strtoupper($vars['email']) == strtoupper($vars['email2'])) )
//		$errors[] = "Looks like your email addresses don't match. Please check again.";
		
	/* Date of Birth */
	
	
	switch( $_GET['price_group'])
	{
		case "1": /* Individual */
			
			$dob = $vars['mdate_month']."/".$vars['mdate_day']."/".$vars['mdate_year'];
			
			$diff = abs(dateDiff("/", $dob, $today ) / 365 );
			if( $diff > 75 )
				$errors[] = "We're sorry. Our Membership program age limit is 75.";
			break;
			
		case "2": /* Individual */
			
			$dob = $vars['mdate_month']."/".$vars['mdate_day']."/".$vars['mdate_year'];
			
			$diff = abs(dateDiff("/", $dob, $today ) / 365 );
			if( $diff > 75 )
				$errors[] = "We're sorry. Our Membership program age limit is 75.";
			break;

		case "5": /* Scholar */
			
			$dob = $vars['mdate_month']."/".$vars['mdate_day']."/".$vars['mdate_year'];
			
			$diff = abs(dateDiff("/", $dob, $today ) / 365 );
			if( $diff > 75 )
				$errors[] = "We're sorry. Our Membership program age limit is 75.";
			break;
			

		case "3": /* Family */
			/* Parse Member */
			$dob = $vars['mdate_month']."/".$vars['mdate_day']."/".$vars['mdate_year'];

			$diff = abs(dateDiff("/", $dob, $today ) / 365 );
			if( $diff > 75 )
				$errors[] = "We're sorry. Our Membership program age limit is 75.";
			
				
			/* Parse Spouse/Parter */
			if( (isset( $vars['spouse_partner_name_f']) && !empty($vars['spouse_partner_name_f']) ) && (isset( $vars['spouse_partner_name_l']) && !empty($vars['spouse_partner_name_l']) ) ) {	
				$dob = $vars['spdate_month']."/".$vars['spdate_day']."/".$vars['spdate_year'];

				$diff = abs(dateDiff("/", $dob, $today ) / 365 );
				if( $diff > 75 )
					$errors[] = "We're sorry. Our Membership program age limit is 75.";
			}
		
			/* Parse Children */
						for( $i=1;$i<6;$i++) {

							if( (isset($vars['c'.$i."_fname"]) && !empty($vars['c'.$i."_fname"]) ) && (isset($vars['c'.$i."_lname"]) && !empty($vars['c'.$i."_lname"])) ) {
								$dob = $vars["c".$i."_mob"]."/".$vars["c".$i."_dob"]."/".$vars["c".$i."_yob"];
								$diff = abs(dateDiff("/", $dob, $today ) / 365 );

								if( $diff > 23 )
									$errors[] = "The age limit for child dependents is 23. Consider one of our Individual Membership plans.";
							}
						}

						break;
			
			case "4": /* Family */
			/* Parse Member */
			$dob = $vars['mdate_month']."/".$vars['mdate_day']."/".$vars['mdate_year'];

			$diff = abs(dateDiff("/", $dob, $today ) / 365 );
			if( $diff > 75 )
				$errors[] = "We're sorry. Our Membership program age limit is 75.";
		
				
			/* Parse Spouse/Parter */
			if( (isset( $vars['spouse_partner_name_f']) && !empty($vars['spouse_partner_name_f']) ) && (isset( $vars['spouse_partner_name_l']) && !empty($vars['spouse_partner_name_l']) ) ) {	
				$dob = $vars['spdate_month']."/".$vars['spdate_day']."/".$vars['spdate_year'];

				$diff = abs(dateDiff("/", $dob, $today ) / 365 );
				if( $diff > 75 )
					$errors[] = "We're sorry. Our Membership program age limit is 75.";
			}
		
			/* Parse Children */
			for( $i=1;$i<6;$i++) {
					
				if( (isset($vars['c'.$i."_fname"]) && !empty($vars['c'.$i."_fname"]) ) && (isset($vars['c'.$i."_lname"]) && !empty($vars['c'.$i."_lname"])) ) {
					$dob = $vars["c".$i."_mob"]."/".$vars["c".$i."_dob"]."/".$vars["c".$i."_yob"];
					$diff = abs(dateDiff("/", $dob, $today ) / 365 );
					
					if( $diff > 23 )
						$errors[] = "The age limit for child dependents is 23. Consider one of our Individual Membership plans.";
				}
			}
			
			break;

	}
		
	return $errors;
}
setup_plugin_hook( 'validate_signup_form', 'vsf');




function get_memstart_date($member_id, $product_id){
    global $db;
    $payments = & $db->get_user_payments(intval($member_id), 1);
    $date = date('Y-m-d');
    $pr_new = $db->get_product($product_id);
    if (preg_match('/^\d\d\d\d-\d\d-\d\d$/', $pr_new['end_date']))
        return $pr_new['start_date']; /// fixed start date
    if ($pr_new['renewal_group'] < 0)
        return $date;
    foreach ($payments as $p){
        $pr = $db->get_product($p['product_id']);
        if ((($p['product_id'] == $product_id) || 
             ($pr['renewal_group'] == $pr_new['renewal_group'])) &&
            ($p['expire_date'] > $date)
            ) 
            $date = $p['expire_date'];
    }
    list($y,$m,$d) = split('-', $date);
    $date = date('Y-m-d', mktime(0,0,0,$m, $d, $y));
    return substr( $date, 5,2)."-".substr( $date, 8,2 )."-".substr( $date, 0, 4 );
}

function gen_member_id( $member_id, $product_id )
{
	$d = get_memstart_date( $member_id, $product_id );
	
	
	$year = substr( (substr( $d, 6, 4 ) - 1), 2, 2);
	
$id = "A08-00".$member_id;
	
	return $id;

//	$id = "A".$year."-00".$member_id;
//	
//	return $id;
} 




function gen_member_name()
{
	$name = "";
	
	if( count($_SESSION['_amember_user']['salutation'] ) == 0 )
		$name .= $_SESSION['_amember_user']['salutation'];
	if( strlen( $_SESSION['_amember_user']['name_f'] ))
		$name .= " ".$_SESSION['_amember_user']['name_f'];
	if( strlen($_SESSION['_amember_user']['middle_initial'] ))
		$name .= " ".$_SESSION['_amember_user']['middle_initial'];
	if( strlen( $_SESSION['_amember_user']['name_l'] ))
		$name .= " ".$_SESSION['_amember_user']['name_l'];
	if( strlen( $_SESSION['_amember_user']['suffix'] ))
		$name .= " ".$_SESSION['_amember_user']['suffix'];
		
	return $name;
}

$LAST_DAYS = Array ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );

function is_leapyear( $year )
{
	if(($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0)
	  	return 1;
	else
		return 0;
}

function gen_expiry( )
{
	global $LAST_DAYS;
	
	$date;
	
	for( $i=0; $i<count($_SESSION['_amember_subscriptions']);$i++)
	{
		if( $_SESSION['_amember_subscriptions'][$i]['product_id'] == substr($_GET['p'], 0, 1) )
		{
			if( is_leapyear(substr($_SESSION['_amember_subscriptions'][$i]['expire_date'], 0, 4) ) && (substr( $_SESSION['_amember_subscriptions'][$i]['expire_date'], 5, 2 ) == 2 ) )  {
				return substr( $_SESSION['_amember_subscriptions'][$i]['expire_date'], 5, 2 ) . "/" . 
					( $LAST_DAYS[(substr( $_SESSION['_amember_subscriptions'][$i]['expire_date'], 5, 2 ) - 1) ] + 1) . 
					"/" . substr( $_SESSION['_amember_subscriptions'][$i]['expire_date'], 0, 4 );
			}
			else {
				return substr( $_SESSION['_amember_subscriptions'][$i]['expire_date'], 5, 2 ) . "/" . 
					$LAST_DAYS[(substr( $_SESSION['_amember_subscriptions'][$i]['expire_date'], 5, 2 ) - 1) ] . 
					"/" . substr( $_SESSION['_amember_subscriptions'][$i]['expire_date'], 0, 4 ); 
			}
		}
	}
	
	return null;
}



?>




Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)
Was This Post Helpful? 0
  • +
  • -

#4 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Age Verification Based on DOB entered by user

Posted 02 July 2008 - 11:29 AM

Gah, can you edit that and put code tags around it.
Was This Post Helpful? 0
  • +
  • -

#5 ellisgl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 161
  • Joined: 10-November 07

Re: Age Verification Based on DOB entered by user

Posted 29 July 2008 - 02:17 PM

I guess he hates old people.. =/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1