• Resolved Shameem Reza

    (@codechef)


    I am adding a text field for user age, and my meta key is: user_age

    Now I want to add a validation message like: Must be between the ages of 21 and 40.

    How can I do it?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Aswin Giri

    (@aswingiri)

    Hello @codechef

    Please follow this article for custom validation.

    Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Hi @codechef

    Here’s a code snippet to validate the age with the birth_date field:

    add_action("um_submit_form_register","um_062821_18_years_old");
    function um_062821_18_years_old( $post_form ){
        
        if(  isset( $post_form['birth_date'] ) && ! empty( $post_form['birth_date'] ) ){
           
           
            // register the error notice
            // $then will first be a string-date
            $then = strtotime( $post_form['birth_date'] );
            //The age to be over, over +18
            $min = strtotime('+18 years', $then);
            if(time() < $min)  {
                UM()->form()->add_error('birth_date', __( 'You should be over 17 years old.', 'ultimate-member' ) );
    		}
    	
        }  
    
    }
    Thread Starter Shameem Reza

    (@codechef)

    Thanks for your kind reply. I am already using those code. Is there any way to limit the maximum age limit as well?

    @codechef

    You can try this code snippet for at least 18 and less than 50 years old:

    add_action("um_submit_form_register","um_062821_18_years_old");
    function um_062821_18_years_old( $post_form ){
        
        if(  isset( $post_form['birth_date'] ) && ! empty( $post_form['birth_date'] ) ){       
            // register the error notice
            // $then will first be a string-date
            $then = strtotime( $post_form['birth_date'] );
            //The age to be over, over +18 and less than +50
            $min = strtotime('+18 years', $then);
            $max = strtotime('+50 years', $then);
            if(time() < $min || time() > $max )  {
                UM()->form()->add_error('birth_date', __( 'You should be at least 18 and less than 50 years old.', 'ultimate-member' ) );
    		}	
        }
    }
    • This reply was modified 3 years, 8 months ago by missveronica.
    Thread Starter Shameem Reza

    (@codechef)

    Thanks @missveronicatv

    I resolved it another way. However, I appreciate your kind reply and solution.

    Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Thanks for letting us know.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom Error Message for Custom Field’ is closed to new replies.