Hello @codechef
Please follow this article for custom validation.
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' ) );
}
}
}
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.
Thanks @missveronicatv
I resolved it another way. However, I appreciate your kind reply and solution.
Thanks for letting us know.