Reply To: I need to validate date_box

Home Forums User Registration I need to validate date_box Reply To: I need to validate date_box

#20671

rumesh38
Moderator

Hi kyde,

You can validate date field by adding the following code in the function.php file of your theme.

function ur_validate_date( $single_form_field, $form_data, $filter_hook, $form_id ) {
	$field_label = isset( $form_data->label ) ? $form_data->label : '';

	if ( 'date_box_1558345242' === $single_form_field->general_setting->field_name ) { // Your field name from form builder.
		if ( ! empty( $form_data->value ) ) {
			$dob  = strtotime( $form_data->value );
			$now  = strtotime( date( 'Y-m-d' ) );
			$diff = ( $now - $dob ) / YEAR_IN_SECONDS;
			if ( 18 > $diff ) {
				add_filter(
					$filter_hook,
					function ( $msg ) use ( $field_label ) {
						return __( $field_label . ' must be 18+.', 'user-registration' );
					}
				);
			}
		}
	}
}
add_action( 'user_registration_validate_date', 'ur_validate_date', 10, 4 );

If you have any query let us know.

Thanks & Regards,
WPEverest Support Team.