Last active
August 21, 2024 18:17
-
-
Save ultimatemember/78aac8b268b2c75489f0 to your computer and use it in GitHub Desktop.
Apply your own custom validation to a field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
In this code sample, you can learn how to apply custom | |
validations on any fields you want. | |
This can be done using these steps: | |
1. Edit the field in backend (field modal) | |
2. Where it asks for field validation choose "Custom" | |
3. Enter a unique validation action name e.g. my_8numbers | |
Now place the following function e.g. in theme functions.php to place | |
your new "my_8numbers" which will apply to that field. | |
in this example, the function registers your new validation and | |
return an error if the input was not a 8-numbers value. this can | |
be useful to test specific formula/validation e.g. a serial number | |
Note how "my_8numbers" is suffix in the function for other validations | |
just change that name, and also change the rules for validation as you like | |
This line: $ultimatemember->form->add_error($key, __('Enter a valid 8-number serial please') ); | |
make sure that form does not submit and return error on screen so user can correct their input. | |
*/ | |
add_action('um_custom_field_validation_my_8numbers','um_custom_field_validation_my_8numbers', 10, 3); | |
function um_custom_field_validation_my_8numbers( $key, $array, $args ) { | |
global $ultimatemember; | |
if ( !ctype_digit( $args[ $key ] ) || strlen( $args[ $key ] ) != 8 ) { | |
$ultimatemember->form->add_error($key, __('Enter a valid 8-number serial please') ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I want to collect phone number in format: + < country-code> -<10 digit phone>.
Please support.