Skip to content

Instantly share code, notes, and snippets.

@ultimatemember
Last active September 13, 2022 11:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ultimatemember/78aac8b268b2c75489f0 to your computer and use it in GitHub Desktop.
Save ultimatemember/78aac8b268b2c75489f0 to your computer and use it in GitHub Desktop.
Apply your own custom validation to a field
/*
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') );
}
}
@huzanspenta
Copy link

huzanspenta commented Jul 4, 2017

Hi,
I want to collect phone number in format: + < country-code> -<10 digit phone>.

Please support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment