Elementor Forms: Check Phone Numbers
Rejects Elementor Pro form submissions whose phone (Tel) field contains letters or too few digits, so you get numbers you can actually call back.
Opens WPIDE on
Code
// Allowed number of digits in a phone number.
$wpide_el_phone_min = 7;
$wpide_el_phone_max = 15;
add_action( 'elementor_pro/forms/validation/tel', function ( $field, $record, $ajax_handler ) use ( $wpide_el_phone_min, $wpide_el_phone_max ) {
$value = isset( $field['value'] ) ? trim( (string) $field['value'] ) : '';
if ( '' === $value ) {
return;
}
$digits = strlen( preg_replace( '/\D/', '', $value ) );
if ( preg_match( '/[^0-9+()\.\-\s]/', $value ) || $digits < $wpide_el_phone_min || $digits > $wpide_el_phone_max ) {
$ajax_handler->add_error( $field['id'], 'Please enter a valid phone number.' );
}
}, 10, 3 );
Notes
Change the minimum and maximum digit counts for your country.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.