I need to validate date_box

Home Forums User Registration I need to validate date_box

This topic contains 2 replies, has 2 voices, and was last updated by  kyde 2 months, 2 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #20630

    kyde
    Participant

    Hello.

    In my site I need to validate that users that wants to register must to be 18+
    please help me.

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

    #20689

    kyde
    Participant

    Hello rumesh38.
    I really apreciate your help, that was so easy, thanks!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.