A Cozmoslabs Product
Documentation / Developer Knowledge Base / Custom Fields / 18+ validation on datepicker field

18+ validation on datepicker field

In case a datepicker with the “date_of_birth” meta-name selects the birth date, this field makes sure that the user is 18+.

add_filter( 'wppb_check_form_field_datepicker', 'wppbc_custom_18_birthdate_validation', 20, 4 );
function wppbc_custom_18_birthdate_validation( $message, $field, $request_data, $form_location ){
    if( $field['field'] == 'Datepicker' && $field['meta-name'] == 'date_of_birth' ){
        if ( isset( $request_data[$field['meta-name']] ) && trim( $request_data[$field['meta-name']] ) != '' ){
            $input = $request_data[$field['meta-name']];

            $birthdate = new DateTime($input);
            $today   = new DateTime('today');
            $age = $birthdate->diff($today)->y;

            if( $age < 18 ) {
                return 'You need to be at least 18 years old to register for our website.';
            }
        }
        if ( ( isset( $request_data[$field['meta-name']] ) && ( trim( $request_data[$field['meta-name']] ) == '' ) ) && ( $field['required'] == 'Yes' ) ){
            return wppb_required_field_error($field["field-title"]);
        }
    }

    return $message;
}

Profile Builder Pro

Create beautiful front-end registration and profile forms with custom fields. Setup member directories, custom redirects, cutomize user emails & more using the all in one user management plugin.

Get Profile Builder

The Ultimate Membership Bundle

Combine the power of Profile Builder with Paid Member Subscriptions to set up user registration, memberships, and recurring revenue.

Get 25% off with the bundle

Help & Support

We’re here to help you every step of the way.

Open a Support Ticket