Age check on a Date field [RESOLVED]

Hi all
I have a Date field (multi fields in DD MM YYYY format).
Im trying to write some custom validation which checks to see if the user is less than 18. Im most of the way there, but I cant seem to get at any of the 3 fields. The date field is 118. When I echo the value out using rgpost it returns ‘Array’. Ive tried getting at it by using rgpost(‘input_118_1’) to get DD, but no success.
What am I doing wrong?
Thanks

Ah. I think Ive got this working now. :slight_smile:

1 Like

Thanks for the update. Please share your solution in case anyone else runs into the same issue. Thanks!

Heres my function to check the date field (in DD MM YYYY) format to see if user’s DOB is younger than 18. (Confusingly, just so happens that my form ID is 18 and my DOB field is 118. Lol)

// check if user is less than 18
add_filter( 'gform_field_validation_18_118', function ( $result, $value, $form, $field ) {

	$dob = rgpost('input_118');
	
	$bday = $dob[0];
	$bmon = $dob[1];
	$byr = $dob[2];

	$stampBirth = mktime(0, 0, 0, $bmon, $bday, $byr);
	$today['day']   = date('d');
	$today['month'] = date('m');
	$today['year']  = date('Y') - 18;
	$stampToday = mktime(0, 0, 0, $today['month'], $today['day'], $today['year']);

	if ($stampBirth > $stampToday) {
		$result['is_valid'] = false;
               $result['message'] = "You must be 18 or older";
}

   return $result;
}, 10, 4 );
1 Like

Thanks for sharing

© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.