/** * Country‐aware phone‐number validation for Elementor forms. * * Assumptions: * - Your phone input’s field ID/key is “phone”. If it’s named differently, update both * $raw_fields['phone'] and add_error( 'phone', … ) accordingly. * - Your hidden country field’s ID/key is “country”, and its value is the two‐letter ISO code * (e.g. “pt”, “es”, “it”, etc.). * * Usage: * 1. Paste into functions.php (or a snippets plugin). * 2. Ensure your Elementor form has: * • A visible text (or tel) field with ID “phone”. * • A hidden field with ID “country” (value = ISO 2‐letter lowercase). * 3. Adjust messages or patterns if your rules change. */ add_action( 'elementor_pro/forms/validation', function( $record, $ajax_handler ) { // 1) Grab submitted fields $raw_fields = $record->get( 'fields' ); // 2) Extract & normalize $phone = isset( $raw_fields['phone']['value'] ) ? preg_replace( '/\D+/', '', trim( $raw_fields['phone']['value'] ) ) : ''; $country = isset( $raw_fields['country']['value'] ) ? strtolower( trim( $raw_fields['country']['value'] ) ) : ''; // 3) Country‐specific regexes, updated per detailed numbering rules $patterns = [ // Portugal: 9 digits total. Landlines start with “2” (2 + 8 digits). Mobiles start with “91x/92x/93x/96x” (9 + 8 digits). 'pt' => '/^(?:2\d{8}|9(?:1|2|3|6)\d{7})$/', // Spain: 9 digits, starts 6–9 'es' => '/^[6-9]\d{8}$/', // Italy: 10 digits. Landline = 0 + nine digits; Mobile = 3 + nine digits. 'it' => '/^(?:0\d{9}|3\d{9})$/', // Romania: 10 digits. Landline = 02|03 + 8 digits; Mobile = 07 + 8 digits. 'ro' => '/^(?:0[23]\d{8}|07\d{8})$/', // Bulgaria: 9 or 10 digits. All begin with 0. 'bg' => '/^0\d{8,9}$/', // Greece: 10 digits. Landline = 2 + nine digits; Mobile = 69 + eight digits. 'gr' => '/^(?:2\d{9}|69\d{8})$/', // Croatia: // • Geographic (9 digits total): // – Zagreb subgroups: 0 + (11–19) + 6 digits // – Other cities: 0 + (20|21|22|23|31|32|33|34|35|40|42|43|44|47|48|49|51|52|53) + 6 digits // • Mobile (10 digits total): // – Prefixes: 91|92|95|98|99 + 7 digits // – bonbon: 976|977 + 6 digits 'hr' => '/^0(?:(?:1[1-9]\d{6}|(?:20|21|22|23|31|32|33|34|35|40|42|43|44|47|48|49|51|52|53)\d{6})|(?:(?:9[1|2|5|8|9]\d{7})|(?:976|977)\d{6}))$/', // Hungary: // Domestic always starts “06” // • Budapest (10 digits): 061 + 7 digits // • Other geographic (10 digits): 06[2–9]\d + 6 digits // • Mobile (11 digits): 06(20|30|31|50|70) + 7 digits 'hu' => '/^06(?:(?:1\d{7})|(?:[2-9]\d\d{6})|(?:(?:20|30|31|50|70)\d{7}))$/', // Slovenia: 9 digits total, always starts 0 + 8 digits 'si' => '/^0\d{8}$/', // Slovakia: // Domestic always starts “0” // • Bratislava (10 digits): 02 + 8 digits // • Other geographic (10 digits): 0(31|32|33|34|35|36|37|38|41|42|43|44|45|46|47|48|51|52|53|54|55|56|57|58) + 7 digits // • Mobile (10 digits): 0(90[1-9]|91\d|94\d|95\d) + 6 digits 'sk' => '/^0(?:(?:2\d{8})|(?:(?:31|32|33|34|35|36|37|38|41|42|43|44|45|46|47|48|51|52|53|54|55|56|57|58)\d{7})|(?:(?:90[1-9]|91\d|94\d|95\d)\d{6}))$/', // Czech Republic: 9 digits total, must start 2, 6, or 7 'cz' => '/^[267]\d{8}$/', // Mexico: 10 digits total 'mx' => '/^[0-9]{10}$/', // Colombia: 10 cifre, cellulare inizia con “3” + 9 cifre 'co' => '/^3\d{9}$/', // Guatemala: 8 cifre, inizia con 2, 3, 4, 5, 6 o 7 'gt' => '/^[2-7]\d{7}$/', // Perù: 9 cifre per i cellulari (iniziano con 9); fissi 7-8 cifre 'pe' => '/^(9\d{8}|[1-8]\d{6,7})$/', // Honduras: 8 cifre. Cellulari = 3, 7, 8, o 9 iniziali 'hn' => '/^[3789]\d{7}$/', // Repubblica Dominicana: 10 cifre, prefisso nazionale = 809, 829, 849 'do' => '/^(809|829|849)\d{7}$/', // Bolivia: 8 cifre. Cellulari iniziano con 6 o 7 'bo' => '/^[67]\d{7}$/', // Panama: 8 cifre. Cellulari iniziano con 6 'pa' => '/^6\d{7}$/', ]; // 4) Localized “please insert a valid number” messages $messages = [ 'pt' => 'Por favor, insira um número válido.', 'es' => 'Por favor, introduzca un número válido.', 'it' => 'Per favore, inserisci un numero valido.', 'ro' => 'Vă rugăm să introduceți un număr valid.', 'bg' => 'Моля, въведете валиден номер.', 'gr' => 'Παρακαλώ εισάγετε ένα έγκυρο αριθμό.', 'hr' => 'Molimo unesite valjani broj.', 'hu' => 'Kérjük, adjon meg egy érvényes számot.', 'si' => 'Prosimo, vnesite veljavno številko.', 'sk' => 'Prosím, zadajte platné číslo.', 'cz' => 'Prosím, vložte platné číslo.', 'mx' => 'Por favor, introduzca un número válido.', 'co' => 'Por favor, ingrese un número válido.', 'gt' => 'Por favor, ingrese un número válido.', 'pe' => 'Por favor, ingrese un número válido.', 'hn' => 'Por favor, ingrese un número válido.', 'do' => 'Por favor, ingrese un número válido con código 809, 829 o 849.', 'bo' => 'Por favor, ingrese un número válido.', 'pa' => 'Por favor, ingrese un número válido.', ]; // 5) If we recognize this country, validate if ( isset( $patterns[ $country ] ) ) { $regex = $patterns[ $country ]; $message = $messages[ $country ]; if ( ! preg_match( $regex, $phone ) ) { // Mark the “phone” field invalid; adjust 'phone' if your field ID differs $ajax_handler->add_error( 'phone', $message ); } } }, 10, 2 );
Vai al contenuto