Created
April 1, 2026 16:35
-
-
Save panstav/1afc8baad8a06a8eb839df9cec2c6380 to your computer and use it in GitHub Desktop.
Elementor tel validation
This file contains hidden or 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
| <?php | |
| // Elementor Form Telephone Number Validation | |
| // Remove default Elementor validation to allow custom formats | |
| add_action( 'elementor_pro/init', 'elementor_form_remove_default_tel_validation' ); | |
| // Add custom validation | |
| add_action( 'elementor_pro/forms/validation/tel', 'elementor_form_tel_validation', 10, 3 ); | |
| /** | |
| * Removes the default Elementor Pro validation for telephone fields. | |
| */ | |
| function elementor_form_remove_default_tel_validation() { | |
| $forms_module = \ElementorPro\Plugin::instance()->modules_manager->get_modules( 'forms' ); | |
| $tel_field = $forms_module->fields_registrar->get( 'tel' ); | |
| if ( $tel_field ) { | |
| remove_action( 'elementor_pro/forms/validation/tel', [ $tel_field, 'validation' ], 10, 3 ); | |
| } | |
| } | |
| /** | |
| * Validates and cleans Elementor Form Telephone fields. | |
| * Supports formats like: 03-966-9911, 054-1231234, +972-54-123-1234. | |
| * Strips invisible LTR/RTL marks from the actual submitted value. | |
| */ | |
| function elementor_form_tel_validation( $field, $record, $ajax_handler ) { | |
| $value = $field['value']; | |
| // Remove invisible LTR (U+200E) and RTL (U+200F) marks | |
| $cleaned_value = preg_replace( '/[\x{200E}\x{200F}]/u', '', $value ); | |
| // If marks were removed, update the actual field value in the record | |
| if ( $cleaned_value !== $value ) { | |
| $record->update_field( $field['id'], 'value', $cleaned_value ); | |
| $value = $cleaned_value; | |
| } | |
| // Basic format: optional +, then digits, then optional (hyphen then digits) repeated. | |
| // No spaces allowed, no consecutive separators, no hyphens at the end. | |
| if ( ! preg_match( '/^\+?[0-9]+(\-?[0-9]+)*$/', $value ) ) { | |
| $ajax_handler->add_error( $field['id'], 'הזן מספר טלפון תקין' ); | |
| return; | |
| } | |
| $digits = preg_replace( '/[^0-9]/', '', $value ); | |
| $len = strlen( $digits ); | |
| // International standard E.164 is up to 15 digits. Shortest numbers are around 7. | |
| if ( $len < 7 || $len > 15 ) { | |
| $ajax_handler->add_error( $field['id'], 'הזן מספר טלפון תקין' ); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment