A solution for Contact Form 7 Phone Field

At long last – don’t know why I didn’t just do this myself…  Keep thinking it will eventually get built into the plugin any day now, but…  Finally a solution to getting a decent phone field validation on Contact form 7!  Click on the title below to go to the original article!

Simple WordPress Contact Form 7 Phone Validation

*Update: July 18, 2012 – the source website seems to be passing Trojans – the link has been disabled!
February 14, 2012 – 4:52 pm I looked around for a simple Contact Form 7 phone validation however all the solutions I found relied on hacking the Contact Form 7 code. By looking through the CF7 code I found a way to attach a simple function to the validation to allow custom validation of any input type you like

To plug into the Contact Form 7 validation there are some filters you can attach to. The built in validation functions do this so we’re copying the standard CF7 way of validation.

All of the code given here can be put into the functions.php file in your wordpress theme.

add_filter( 'wpcf7_validate_text', 'drc_wpcf7_validate_text' , 10, 2 );
add_filter( 'wpcf7_validate_text*', 'drc_wpcf7_validate_text' , 10, 2 );

This code attaches to two filter calls – one for a text field (wpcf7_validate_text) and one for a required text field (wpcf7_validate_text* – note the asterisk at the end).

function drc_wpcf7_validate_text( $result, $tag ) {
$type = $tag['type'];
$name = $tag['name'];
$value = $_POST[$name] ;
 
if ( strpos( $name , 'phone' ) !== false ){
$regex = '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?< =\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/';
$Valid = preg_match($regex,  $value, $matches );
	if ( $Valid > 0 ) {
		$formatted = "($matches[1]) $matches[2]-$matches[3]";
		if ($matches[4]) $formatted .= " x$matches[4]";
		// Replace the value passed in with the cleaned up version.
		$_POST[$name] = $formatted ;
	}
	else {
		$result['valid'] = false;
		$result['reason'][$name] = 'Phone field invalid';
	}
    }
}

This simple routine gets called to validate all text fields in your form. The strpos looks for a field name containing ‘phone’ i.e. ‘your-phonenumber’ and will validate that. This is very simple to extend to Zip codes or any other text field you would like validated. By setting $result[‘valid’] to false we tell Contact Form 7 that the validation failed, the value you set for $result[‘reason’][$name] is the message that is displayed to the user of the site.

No related posts.

Author: Eric Erickson

Share This Post On

6 Comments

  1. Thank you so much for the solution, you are a life saver, this is exactly what I was looking for and very simple and straight forward. Good thing is that I can upgrade my contact form 7 plugin and it won’t effect this custom code.

    Post a Reply
  2. I think you forgot to add return $result at the end of the drc_wpcf7_validate_text method ?

    Post a Reply
    • Thamsanga – i copied that code and got no syntax error.
      What is wrong with the solution?

      Post a Reply
  3. Here is a full validation for contact form 7 – phone, email, date comaprision, us zip ,canada zip etc. You can modify the addon according your requirement too. It’s very easy if you know basic php. The plugin version is not yet launched. You need to include the file in your code though.

    Post a Reply
  4. Great content! I will like to extend your script to block some kind of numbers starting with 7045* and 07045*, could you give a sample add on code? Thank you.

    Post a Reply
    • Yikes, I can’t say with any authority on that question. While I did play with contact form 7 there for a while, they have now updated it and I really haven’t mucked with it. Besides that, I got into far too many sites to go with any custom coding on these modules. Now if it was for just a single site, I would probably still do it, but when you get over 100 sites out there, trying to remember which plugins/code you jimmied to get something to work gets iffy and led to a whole LOT of – “Aw crap” moments.

      Post a Reply

Leave a Reply to Thamsanqa Cancel reply

Your email address will not be published. Required fields are marked *