Fields Validation
There are several ways to validate a field value:
- Select a predefined validation option in the field setting Validate.
- Select the Custom Validation option in the Validate setting, then type a keyword for the custom validation hook in the Custom Action setting. Attach your custom validation function to the hook
um_custom_field_validation_{custom_action}
, where{custom_action}
is a keyword in the Custom Action setting. - Attach your custom validation function to the hook
um_submit_form_errors_hook_
.
Note: You can add your custom PHP code to the functions.php file in the active theme directory.
Validation options
A list of predefined validation rules:
- Alphabetic value only
- Google URL
- VK URL
- Alpha-numeric value
- English letters only
- Facebook URL
- Instagram URL
- LinkedIn URL
- Lowercase only
- Numeric value only
- Phone Number
- Skype ID
- SoundCloud Profile
- Twitter URL
- E-mail( Not Unique )
- Unique E-mail
- Unique Metakey value
- Unique Username
- Unique Username/E-mail
- Website URL
- YouTube Profile
- Telegram URL
- Discord ID
- Custom Validation
Image - Field settings, predefined validation options.
Custom Validation option
You can select the Custom Validation option in the Validate setting and add your custom validation function to the functions.php file in the active theme directory. Attach your custom validation function to the hook um_custom_field_validation_{custom_action}
, where {custom_action}
is a keyword in the Custom Action setting.
Image - Field settings.
Code example - Custom validation function attached to the custom action hook.
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 );
Image - Error message in the form.
Hook um_submit_form_errors_hook_
You can use the um_submit_form_errors_hook_
hook to apply custom validations and rules for any fields and stop forms from being submitted until the conditions are met.
In the example below we check that the username field does not have the word "admin".
Code example - Custom validation function attached to the hook.
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.' ); } } add_action('um_submit_form_errors_hook_','um_custom_validate_username', 999, 1);
Custom Validating with two fields
Examples below will help you validate and compare two fields. We use "user_login" & "nickname" as examples. The user will get an error message if the values in both fields are the same.
Example with the Custom Validation option
Image - Field settings
Code example - Custom validation function attached to the custom action hook.
function um_custom_validate_username_nickname( $key, $array, $args ) { if ( isset( $args[$key] ) && isset( $args['user_login'] ) && $args[$key] == $args['user_login'] ) { UM()->form()->add_error( $key, __( 'Your username and nickname can not be equal.', 'ultimate-member' ) ); } } add_action( 'um_custom_field_validation_username_nickname', 'um_custom_validate_username_nickname', 30, 3 );
Image - Error message in the form.
Example with the hook um_submit_form_errors_hook_
Code example - Custom validation function attached to the hook.
function um_custom_validate_username_nickname( $args ) { if ( isset( $args['user_login'] ) && isset( $args['nickname'] ) && $args['user_login'] == $args['nickname'] ) { UM()->form()->add_error( 'user_login', 'Your username and nickname can not be equal.' ); } } add_action('um_submit_form_errors_hook_', 'um_custom_validate_username_nickname', 999, 1);
Image - Error message in the form.