/**
* Validate phone format for israeli numbers.
*
* Supports:
* XX-XXXXXXX
* XXX-XXXXXXX
* XXXXXXXXX
* XXXXXXXXXX
*
* @param array $field Form field.
* @param Form_Record $record An instance of the form record.
* @param Ajax_Handler $ajax_handler An instance of the ajax handler.
*/
function dorzki_validate_phone_elementor( $field, $record, $ajax_handler ) {
if ( preg_match( '/^((0\d{1,2}\-\d{7})|(0\d{8,9}))$/', $field['value'] ) !== 1 ) {
$ajax_handler->add_error( $field['id'], esc_html__( 'Phone number format is invalid, please enter the phone number without spaces and special chars.', 'dorzki' ) );
}
}
add_action( 'elementor_pro/forms/validation/tel', 'dorzki_validate_phone_elementor', 10, 3 );
How to use it?
Usually adding functionality to a WordPress site is by creating a plugin.
However you can simply copy the code and paste it in your child theme’s functions.php
file at the end of the file, just before ?>
.
For JavaScript code you will need to add them to your theme’s main JavaScript file.
Having issues with the code?
If the code doesn’t work or you get an error please let us know in the comments section.
We will do our best to fix it as-soon-as-possible and update the code on this page.
Comments
Hi there!
I really like your code and it works pretty well for me, BUT –
In my case I have phone numbers that starts with: “08” and it seems that your code doesn’t support this sequence.
How can I tweak this code so it will also considered numbers that starts with: “08”?
Thanks!
Hi Moria,
The code does support local numbers like 08, 04, etc.
Thank you for this solution,
Phone numbers in Israel are now 10 digits, therefore the code should be without the 8 digits at this line, this so:
if ( preg_match( ‘/^((0\d{1,2}\-\d{7})|(0\d{9}))$/’, $field[‘value’] ) !== 1 ) {
Had to make this change because phones without the full 10 digits were sent.
Correct me if I’m wrong 🙂
Hi Michal,
This is incorrect, the code does support both mobile and local numbers, please check again 🙂
Worked perfectly