Add Number Validation in Contact Form 7 WordPress Plugin

Today one of my friend ask me about number validation in “Contact Form 7” WordPress plugin

I wish to have a number validation for my text inputs. I will be using it for contact numbers. Only email and text only validations exist here. Please help me and thank you very much.

First of all I would like to note that the plugin is very well written, it’s veryflexible, and only due to its flexibility it’s allowed me to extend its functionality. Of course Takayuki probably missed some validations that other plugin and theme developers would love to see, but the modules directory is good enough, despiting the fact that it’ll probably be erased and rewritten after an ongoing plugin update.

Steps to Add Phone Validation:

Open the “contact-form-7” folder located in your wp-contents/plugins folder.

STEP 1. Open “contact-form-7/modules/text.php”:

insert this code:
wpcf7_add_shortcode( 'number', 'wpcf7_text_shortcode_handler', true );
wpcf7_add_shortcode( 'number*', 'wpcf7_text_shortcode_handler', true );

after this code:
wpcf7_add_shortcode( 'email*', 'wpcf7_text_shortcode_handler', true );

insert this code:
if ( 'number' == $type || 'number*' == $type )
$class_att .= ' wpcf7-validates-as-digit';

after this code:
if ( 'text*' == $type || 'email*' == $type )
$class_att .= ' wpcf7-validates-as-required';

insert this code:
add_filter( 'wpcf7_validate_number', 'wpcf7_text_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_number*', 'wpcf7_text_validation_filter', 10, 2 );

after this code:
add_filter( 'wpcf7_validate_email', 'wpcf7_text_validation_filter', 10, 2 );
insert this code:
if ( 'number' == $type || 'number*' == $type ) {
   if ( 'number*' == $type && '' == $_POST[$name] ) {
      $result['valid'] = false;
      $result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
   } elseif ( '' != $_POST[$name] && ! is_validNumber( $_POST[$name] ) ) {
      $result['valid'] = false;
      $result['reason'][$name] = wpcf7_get_message( 'invalid_number' );

after this code:
if ( 'email' == $type || 'email*' == $type ) {
   if ( 'email*' == $type && '' == $_POST[$name] ) {
      $result['valid'] = false;
      $result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
   } elseif ( '' != $_POST[$name] && ! is_email( $_POST[$name] ) ) {
      $result['valid'] = false;
      $result['reason'][$name] = wpcf7_get_message( 'invalid_email' );


STEP 1a. Generator Tag for this Number Field:

Now, You need to add a new tag to the Number fields. This tag should correspond to the tag in the Form you wish to include in the number field. Thus, when you add a new form tag such as [number your-new-field], the corresponding tag would be [your-new-field]. Only the second part of the tag (“your-new-field”, in this case) is needed for the number field. A tag in the Number field will be replaced with the user’s input contained in a form field with the same name.

insert this code in Function wpcf7_add_tag_generator_text_and_email():
wpcf7_add_tag_generator( 'number', __( 'Number field', 'wpcf7' ),
   'wpcf7-tg-pane-number', 'wpcf7_tg_pane_number' );

after this code:
wpcf7_add_tag_generator( 'email', __( 'Email field', 'wpcf7' ),
   'wpcf7-tg-pane-email', 'wpcf7_tg_pane_email' );
insert new function:
function wpcf7_tg_pane_number( &$contact_form ) {
   wpcf7_tg_pane_text_and_email( 'number' );

after this code:
function wpcf7_tg_pane_email( &$contact_form ) {
   wpcf7_tg_pane_text_and_email( 'email' );
replace these lines:
function wpcf7_tg_pane_text_and_email( $type = 'text') {
 if ( 'email' != $type )
 $type = 'text';

as this:
function wpcf7_tg_pane_text_and_email( $type ) {
   //if ( 'email' != $type )	$type = 'text';
   //elseif ( 'number' != $type )	$type = 'text';
   if ($type == "")	$type = 'text';


STEP 2. Open “contact-form-7/modules/functions.php” to add the error message for the phone number validation:

insert this code:
'invalid_number' => array(
   'description' => __( “There is a field that sender is needed to fill in with numbers”, 'wpcf7′ ),
   'default' => __( 'Please fill the required field with numbers.', 'wpcf7′ )

after this code:
'invalid_required' => array(
   'description' => __( “There is a field that sender is needed to fill in”, 'wpcf7′ ),
   'default' => __( 'Please fill the required field.', 'wpcf7′ )


STEP 3. Open “contact-form-7/modules/formatting.php” and create a new function:

function is_validNumber($chkNum) {
   $regexp = '/^[0-9\+\-\)\(\d\s]{7,}$/';
   if(preg_match($regexp, $chkNum))
      return true;
      return false;


How to use this Tag :

In above code, I created a function wherein the input text for the phone number will accept minimum of 7 characters (this only includes numbers and characters + and -). To use this tag in the contact form 7, please use this tag just Your Phone or use Tag Generator from right side.

[digit* text-417]

Example of valid inputs are +1234567, 01234567 or 02-1234567

Naseer Ahmad

I am Naseer Ahmad Mughal from Islamabad (Pakistan). I have been working as a Manager Development at SyntecX from last 4+ years, and its my passion to learn new things and implement them as a practice. I am also work as freelancer as well as a volunteer web development expert who loves to learn through innovative ideas and inspiration. You can find me online at LinkedIn & Twitter