Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi there @palomarmodular ,

    This code would be placed in your theme’s (or child theme’s) functions.php file. Be sure that you have access to FTP just in case something was to go wrong then you could remove this snippet. Best of luck!

    Hi There,

    I am not sure if you are still needing this but I figured I would post my solution for anyone who finds this. I have it setup to reject when numbers are found in any of our name fields but you could easily modify the preg to check for other things as well.

    /*SPAM VALIDATION FOR NAME FIELD IN CF7*/
    add_filter('wpcf7_validate_text*', 'name_validation_filter', 20, 2);
    
    function name_validation_filter($result, $tag) {
        //Get The Tag
        $tag = new WPCF7_FormTag($tag);
        //Check if the text field is the name
        if ('your-name' == $tag->name || 'name' == $tag->name || 'first-name' == $tag->name ||  'last-name' == $tag->name)
        {   
            //Set the possible name variables
            $your_name = isset($_POST['your-name']) ? trim($_POST['your-name']) : '';
            $name = isset($_POST['name']) ? trim($_POST['name']) : '';
            $first_name = isset($_POST['first-name']) ? trim($_POST['first-name']) : '';
            $last_name = isset($_POST['last-name']) ? trim($_POST['last-name']) : '';
            //Check if any of the name variables have numbers
            if (preg_match('#[0-9]#', $your_name) || preg_match('#[0-9]#', $name) || preg_match('#[0-9]#', $first_name) || preg_match('#[0-9]#', $last_name))
            {
              // Fail validation and set validation error message
              $result->invalidate($tag, "Please input your name.");
            }
        }
        return $result;
    }
Viewing 2 replies - 1 through 2 (of 2 total)