• Resolved wpress2010

    (@wpress2010)


    I want to make a CF7 form that has one field where a user must enter a specific text string or number in order for the form to submit successfully. This would be for a secure way that members of a nonprofit organization could cast their proxy votes for an election. If they enter the predetermined number or text string correctly, the form submits. Otherwise, error, and no submission.

    I know that there’s a way to do this with an entry in the functions.php file, as it involves the validation hooks that already exist in CF7. The examples I’ve looked at only seem to address things like validating field entries that are only numbers, or only text, or only a telephone number, etc. I just want to validate an exact number or string. I know there’s a CF7 plugin using jQuery for this, but I prefer just adding the necessary code myself without clogging up the site admin with more plugs.

    My PHP knowledge is not all that great, but I’ve been able to handle changes to the functions.php file fairly well.

    Can anyone give me an example of the type of validation I’m trying to achieve?

    TIA for your assistance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Thread Starter wpress2010

    (@wpress2010)

    Thank you! I came up with this code and it works perfectly (the proxy code is set to “tomato”):

    function custom_proxy_validation($result,$tag){
            $type = $tag['type'];
            $name = $tag['name'];
            if($name == 'proxynumber'){
               $proxyNumber = isset( $_POST['proxynumber'] ) ? trim( $_POST['proxynumber'] ) : '';
    										
                $the_value = preg_match("/tomato/",$_POST[$name]);
                if($proxyNumber == "" || $the_value == false ){ 
                    $result->invalidate( $tag, "please enter valid proxy number" );
                }
            }
            return $result;
        }
        add_filter('wpcf7_validate_text','custom_proxy_validation', 10, 2);
        add_filter('wpcf7_validate_text*', 'custom_proxy_validation', 10, 2);
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom field validation question’ is closed to new replies.