How to modify international phone validation
-
Hi! Is there a way in which I can achieve the following:
- Add an extra number to a specific country code, in this case Argentina. It’s +54 but it’d like it to be +549 because a 9 is needed when mobile phones are entered. At the same time, I’d like people not to be able to remove this (currently they can)
- Remove or not allow spaces in the field, as well as no extra characters except for ‘+’. This is so the numbers I collect have no spaces or hyphens.
At the moment, I am using the input field with a pre-filled country code +549 and I allow no more than 14 characters, but this is not ideal as there is no validation.
Thanks a lot!
-
Hello @matias1234
Hope you’re doing well today! Thank you for reaching out to us.
What you’re trying to achieve on the phone field set with international validation is something that is not present out of the box.
However, I am checking with our developer team to see if there is any possible workaround that can be shared to achieve both scenarios. We will update you here as soon as we have further information from them.
Kind Regards,
SaurabhOk, thanks very much!!
Hi @matias1234,
Could you please try this snippet and then check whether it helps?
<?php add_action( 'wp_footer', 'wpmudev_phone_field_country_addition', 9999 ); function wpmudev_phone_field_country_addition() { global $post; if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) { return; } ?> <script type="text/javascript"> jQuery( document ).ready(function($){ setTimeout(function() { $('.forminator-custom-form').trigger('after.load.forminator'); }, 500); $(document).on('after.load.forminator', function(e, form_id) { if ( 'forminator-module-2960' == e.target.id ) { // Please change 2960 to your form's ID. $('.forminator-field--phone').each(function(index) { var phone_ival = $(this).val(); if ( phone_ival == '+54' ) { $(this).val( phone_ival + '9' ); } $(this).on('input', function(){ var phone_val = $(this).val(); if ( phone_val.startsWith('+54') ) { phone_val = phone_val.replace(/\s/g, ''); var title = $('.iti__selected-flag').attr('title'); var code = title.split('+').pop(); phone_val = phone_val.replace('+'+code+'9', ''); phone_val = phone_val.replace('+'+code, ''); $(this).val('+'+code+'9'+phone_val); } }); }); } }); }); </script> <?php }
Please do make sure to update the line 2960 in the above code with your form ID.
You can implement the above code using mu-plugins. Please check this link on how to implement the above code as a mu-plugins:
https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-pluginsPlease do let us know how that goes.
Kind Regards,
Nithin
Hi there, thanks very much! It works just as I expected it! I appreciate it!
How can I apply this to more than one form, or to all forms if it′s easier? I have created different mu-plugins with different “forminator-module-” numbers but the site crashes if I add more than one.
Thanks a lot!As an update, I managed to do the above, with the following code provided in another thread, but it’s not working for a Leads Form in a quiz. That is, it works for all Forminator Forms but it doesn’t work with Leads From in a quiz. I tried both adding the ID of the quiz and of the Leads Form. Am I missing anything?
if ( e.target.id == 'forminator-module-6'
|| e.target.id == ‘forminator-module-7’ || e.target.id == ‘forminator-module-8’) {
Hi @matias1234,
We have asked our Second Level Support team to check if the code could be further tweaked to include the lead forms. We’ll update you here once we have more feedback on this as soon as possible.
Kind Regards,
Nebu JohnHi @matias1234,
I hope you are doing well today!
Our SLS team updated the code snippet as belows;
<?php add_action( 'wp_footer', 'wpmudev_phone_field_country_addition', 9999 ); function wpmudev_phone_field_country_addition() { global $post; if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) && ! has_shortcode( $post->post_content, 'forminator_quiz' ) ) { return; } ?> <script type="text/javascript"> jQuery( document ).ready(function($){ setTimeout(function() { $('.forminator-custom-form').trigger('after.load.forminator'); }, 500); $(document).on('after.load.forminator', function(e, form_id) { if ( 'forminator-module-2960' == e.target.id || 'forminator-module-1889' == e.target.id ) { // Please change 1889, 2960 to your form's ID. $('.forminator-field--phone').each(function(index) { var phone_ival = $(this).val(); if ( phone_ival == '+54' ) { $(this).val( phone_ival + '9' ); } $(this).on('input', function(){ var phone_val = $(this).val(); if ( phone_val.startsWith('+54') ) { phone_val = phone_val.replace(/\s/g, ''); var title = $('.iti__selected-flag').attr('title'); var code = title.split('+').pop(); phone_val = phone_val.replace('+'+code+'9', ''); phone_val = phone_val.replace('+'+code, ''); $(this).val('+'+code+'9'+phone_val); } }); }); } }); }); </script> <?php }
You can find more information below on how to use mu-plugins.
https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins
and
https://www.ads-software.com/support/article/must-use-plugins/It would be better to test this out on your staging/dev environment first before your live site and make sure you have the latest backups and change Form ID in the code.
Kind regards,
ZaferHi, thanks very much! Unfortunately, it′s not working for my Leads form in my quiz. The new code produces the same result as the previous one. I tried using the id number of the quiz and of the lead form, and both, but no combination seems to work. It does, however, create the desired effect for the regular forms.
If there was anything that maybe modified in the code, I′d appreciate it.
Here goes the code I′m using as a mu-plugin<?php add_action( 'wp_footer', 'wpmudev_phone_field_country_addition', 9999 ); function wpmudev_phone_field_country_addition() { global $post; if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) && ! has_shortcode( $post->post_content, 'forminator_quiz' ) ) { return; } ?> <script type="text/javascript"> jQuery( document ).ready(function($){ setTimeout(function() { $('.forminator-custom-form').trigger('after.load.forminator'); }, 500); $(document).on('after.load.forminator', function(e, form_id) { if ( 'forminator-module-14958' == e.target.id || 'forminator-module-20081' == e.target.id || 'forminator-module-20186' == e.target.id || 'forminator-module-16955' == e.target.id || 'forminator-module-16956' == e.target.id) { // Please change 1889, 2960 to your form's ID. $('.forminator-field--phone').each(function(index) { var phone_ival = $(this).val(); if ( phone_ival == '+54' ) { $(this).val( phone_ival + '9' ); } $(this).on('input', function(){ var phone_val = $(this).val(); if ( phone_val.startsWith('+54') ) { phone_val = phone_val.replace(/\s/g, ''); var title = $('.iti__selected-flag').attr('title'); var code = title.split('+').pop(); phone_val = phone_val.replace('+'+code+'9', ''); phone_val = phone_val.replace('+'+code, ''); $(this).val('+'+code+'9'+phone_val); } }); }); } }); }); </script> <?php }
The first three are forms (14958, 20081 and 20186 and are working OK. 16956 is the lead form of the quiz, and 16955 is the quiz per se)
Thanks very much!Hi @matias1234
Thanks for response!
The ID in the code should be an ID of the form and not the quiz – so it’s an ID of the lead generation form used on the quiz.
As for the form itself, I’m not quite sure yet – I tried that code to replicate the issue but I actually had quite “mixed” results here. It seems I’m not sure how the phone field is configured exactly.
Can you specify what are the exact settings of the phone field? You mentioned earlier that you “allow only 15 characters” which would suggest no validation other than character limit, then my colleague mentioned “international validat” – but that doesn’t specify character limit so I’m not sure which one is that.
I’d like to replicate that first.
Kind regards,
AdamHi! Thanks for the reply. I′m using a Phone field with international validation in all cases. In the forms, the code works as expected, but in the Leads form it does not.
What I mentioned about the characters was a workaround I used before your solution and code were proposed – I′m not using that any longer.Hi @matias1234,
I tested the snippet in my test site and it works fine. Please do note that it’ll only change when you fill in the phone number, is that how you are testing?
Possible to share a page URL where you have the Quiz added so that we can check and compare the code and see what’s missed?
Looking forward to your response.
Kind Regards,
Nithin
Hi! Thanks for the reply. I understand what you′re saying but the snippet is not working for my Lead form.
This is the URL with the lead form (beginning of the quiz): https://www.whitecastle.com.ar/nivel/test_adultos/This is my contact page where you can see a regular form where the snippet works as expected:
https://www.whitecastle.com.ar/contactoThanks very much
Hi @matias1234,
I checked the website but I don’t see any console errors that would point the code might not work. It should have worked fine as the same snippet works in my test sites Lead Form:
View post on imgur.com
If the code is as exact you have shared in your previous response ie:
https://www.ads-software.com/support/topic/how-to-modify-international-phone-validation/#post-17752368Then it should have worked fine out of the box. Do you have a test or staging site? If yes, possible to only keep Forminator and test whether it works in there with Quiz Leads?
I noticed the site has LiteSpeed enabled and the pages are minified, there could be a high chance the issue might also be due to minification.
Could you please temporarily disable the LiteSpeed plugin and clear the cache and see if you still get the same issue?
Please do let us know how the above goes so that it would help give a better idea.
Kind Regards,
NIthin
Hi! Thanks very much for your reply. After performing several tests, the code is still not working on my existing Leads form. However, if I create a new Quiz with a new Leads form, it does work!
Unfortunately, I can′t create a whole new quiz as it’d be time consuming as there’s no export or duplicate feature available for quizzes.
Any idea why it’s not working for an existing Leads form? Is there anything I could do, like clearing any cached files?
Thanks a lot!
Hello @matias1234,
Thank you for the explanation. It looks like the quiz takes a bit longer to load, compared to the forms.
Could you please try adjusting 500 timeout value at the beginning:
setTimeout(function() { $('.forminator-custom-form').trigger('after.load.forminator'); }, 500);
For example, try increasing it to 1000 – 2000 or more:
setTimeout(function() { $('.forminator-custom-form').trigger('after.load.forminator'); }, 2000);
Hope this helps. Let us know if there’s still any issue.
Best Regards,
Dmytro
- You must be logged in to reply to this topic.