Regex not filtering out what I expect it to
-
I have a custom filter function which is supposed to filter out all telephone numbers not beginning with either a 2 followed by 9 digits or a 69 followed by 8 digits. The function is supposed to be validating a form with a tel* field named “Telephone”. What I’ve put in my contact form is the following:
[text Name id:Name_ba placeholder "Ονοματεπώνυμο"] [tel* Telephone id:Telephone_ba placeholder "Τηλέφωνο *"] [email Email id:Email_ba placeholder "Το email σου"]
What I’ve put in the functions.php of my child theme is the following:
add_filter('wpcf7_validate_tel', 'custom_phone_validation', 20, 2); add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 20, 2); function custom_phone_validation($result, $tag) { $type = $tag->type; $name = $tag->name; if($name == 'Telephone') { $value = $_POST[$name]; if(!preg_match("/^(2)[0-9]{9}$|^(69)[0-9]{8}$/i", $value )){ $result->invalidate($tag, "Φαίνεται σα να μην πληκτρολογήσατε ένα έγκυρο τηλέφωνο."); } } return $result; }
I have tested with a text input on the tel field, and in that case the filter works as expected. However when I enter a phone like 9925142491 it’s not.
What is it that I am doing wrong?
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Regex not filtering out what I expect it to’ is closed to new replies.