Apply custom validation to a field

You can use UM action hooks to apply custom validations and rules for any fields and stop form from being submitted until the conditions are met. In this example, we check that the username field does not have the word admin.

add_action('um_submit_form_errors_hook_','um_custom_validate_username', 999, 1);
function um_custom_validate_username( $args ) {
	
	if ( isset( $args['user_login'] ) && strstr( $args['user_login'], 'admin' ) ) {
		UM()->form()->add_error( 'user_login', 'Your username must not contain the word admin.' );
	}
}

You also can select "Custom Validation" and add your "Custom Action" PHP code to the functions.php file in the active theme directory.

Use hook um_custom_field_validation_{$custom} to attach "Custom Action" PHP code.

Code example:

/**
 * Validate field Mobile Number
 * @param string $key
 * @param attay  $array
 * @param array  $args
 */
function um_custom_validate_mobile_number( $key, $array, $args ) {
	if ( isset( $args[$key] ) && !preg_match('/^[6-9]\d{9}$/', $args[$key]) ) {
		UM()->form()->add_error( $key, __( 'Please enter valid Mobile Number.', 'ultimate-member' ) );
	}
}
add_action( 'um_custom_field_validation_mobile_number', 'um_custom_validate_mobile_number', 30, 3 );

Screenshots:

[Ultimate Member > Forms > Edit Form > Edit Field]

Profile form, Error messege