• After much reading, I’ve found a way to validate a CF7 textfield for “numbers only”.

    One doesn’t have to hack the plugin source files to achieve this, which is good.

    You will have to edit the function code to suit your needs.

    Always test locally, not on a live website.

    Step one:
    Add this code to your theme’s function.php (wp-content/themes/your-theme/functions.php)

    /*
    Validate Numbers in Contact Form 7
    This is for 10 digit numbers
    */
    
    function is_number( $result, $tag ) {
    $type = $tag['type'];
    $name = $tag['name'];
    
    if ($name == 'phone' || $name == 'fax') { // Validation applies to these textfield names. Add more with || inbetween
    $stripped = preg_replace( '/\D/', '', $_POST[$name] );
    $_POST[$name] = $stripped;
    if( strlen( $_POST[$name] ) != 10 ) { // Number string must equal this
    $result['valid'] = false;
    $result['reason'][$name] = $_POST[$name] = 'Please enter a 10 digit phone number.';
    }
    }
    return $result;
    }
    
    add_filter( 'wpcf7_validate_text', 'is_number', 10, 2 );
    add_filter( 'wpcf7_validate_text*', 'is_number', 10, 2 );

    Step 2:
    Create 2 normal, required textfields with the name ‘phone’ and ‘fax’ in your CF7 form. Example:

    <p>Phone Number:<br />
    [text* phone] </p>
    
    <p>Fax Number:<br />
    [text* fax] </p>

    That’s it, not very refined but works for me.

    http://wordpress.org/extend/plugins/contact-form-7/

Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Number / Phone Validation for Contact Form 7 – Doing it Manually’ is closed to new replies.