1

Thank you for paying attention to my question.

I am making a website for myself on WordPress, in parallel I am learning programming. I ask you to help sort out one point. I have implemented a contact form based on the Contact Form 7 plugin. I connected JS (cleave.js) to make it convenient to enter a phone number (only numbers and in a certain amount are entered). Everything works fine except for one thing: the field for entering the phone number stops working if the same contact form was already launched somewhere on the site. i.e. the same contact form is found on different pages of the site (for example: on the home page and on the internal page). On the home page, the field for entering the number works correctly, but on the internal page the conditions for the phone number field do not work.

Please tell me how to fix this so that the conditions for the number input field are triggered not only when opening the site, but on all pages where there is a contact form. The author advises using a loop by running instance.getRawValue (). Maybe there is another solution or JS that is easier to use.

Thanks!

JS link: https://github.com/nosir/cleave.js

Used code:

function enqueue_cleave(){
     wp_enqueue_script('cleave', get_stylesheet_directory_uri().'/js/cleave.min.js');
}
add_action('wp_enqueue_scripts', 'enqueue_cleave'); 

add_action('wp_footer', 'activate_cleave');

function activate_cleave(){
   if( is_page('') ){
?>

     <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', () => {
        var phone = new Cleave('#phone', {
            numericOnly:true,
            prefix:'+',
            delimiter:'-',
            blocks:[5,4,4,2]
        });
    });

     </script>

<?php 
   }
}
Bergaul
  • 11
  • 2
  • Can you check the google chrome dev tools on the page that it's not working? There must be an error. To open dev tools right click anywehere on the page and click Inspect. – Ozgur Sar Nov 11 '20 at 18:02
  • Hello! Thanks for your feedback. I encountered the following error: [cleave.js] Multiple input fields matched, cleave.js will only take the first one. i @ cleave.min.js?ver=5.5.3:8 (anonymous) @ (index):1756 – Bergaul Nov 20 '20 at 07:01
  • Are there more than 1 phone fields with the matching ID `#phone` ? – Ozgur Sar Nov 20 '20 at 07:26
  • Hello! Thanks for your feedback. there is only 1 ID #phone in one contact form. This contact form is duplicated on several pages: on the home page, in the "contacts" section. – Bergaul Nov 25 '20 at 18:06

0 Answers0