• Resolved davidami

    (@davidami)


    i would like to validate a text tag in contact form 7, to validate an italian car plate, this is my RegExp in JavaScript:

    <script>
    function targaCheck(){
    var targaRExp = /^(([a-zA-Z]{2}\d{3}[a-zA-Z]{2})|(([a-zA-Z]{2}|roma)(\d{5}|\d{6})))$/i;
    var targa = document.getElementById(“targa”).value;
    if( targaRExp.test(targa) ) {
    alert(“Correct”);
    }else {
    alert(“Incorrect”);
    }
    }
    </script>

    To validate the contact form 7 i have to use “add_filter” in function.php of my WP site, but when i insert my code it doesn’t work. This is my code into function.php

        //add filter for text field validation
    include("cf7-custom-validation.php");
    add_filter('wpcf7_validate_text*', 'cf7_custom_form_validation', 10, 2); // Req. text field
    
    // TARGA ITALIANA
    
        if($name == 'targa') {
            $targa = $_POST['targa'];
    
            if($targa != '') {
                    if(!preg_match('/^(([a-zA-Z]{2}\d{3}[a-zA-Z]{2})|(([a-zA-Z]{2}|roma)(\d{5}|\d{6})))$/i', $targa)) {
                    $result['valid'] = false;
                    $result['reason'][$name] = 'La Targa inserita non è valida';
                }
            }
        }

    Where am I wrong?

    Please help me

    Thanks!

  • The topic ‘Custom validation Contact Form 7’ is closed to new replies.