Skip to content
Advertisement

How to include JS in Contact Form 7 In WordPress

I have this piece of code mine that i have problem with. I hope you guys can help me on this matter. What i want to do here is to validate the [input text] of Contactform 7 that user can type only number’s on the input. I have this as the reference Including JS to Contact Form 7 WordPress on doing this. So i want to create a shortcode of this script on my functions php then use this shortcode in the contact form as suggested on the link above. This is the error I’m currently getting on this code in my functions.php

Your PHP code changes were rolled back due to an error on line 262 of file wp-content/themes/capitall/functions.php. Please fix and try saving again. syntax error, unexpected ‘<‘

function input_cf7() {

<script>

        var userName = document.querySelector('#numberField');

        userName.addEventListener('input', restrictNumber);
        function restrictNumber (e) {  
        var newValue = this.value.replace(new RegExp(/[^d]/,'ig'), "");
        this.value = newValue;

        }

</script>

}

add_shortcode('input_validation', 'input_cf7');

Advertisement

Answer

The script tag is in html it will not run in php block so you have to close your php block before script tag and open php block after script tag

enter image description here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement