Friday, October 17, 2014

Contact form 7 telephone number digits restriction modification

If you use the contact form 7 plugin (version 4.0) for contact information gathering on your wordpress site and want to restrict the minimum and maximum digits a user enters but without sacrificing the brackets, spaces or plus sign here's how I found out to do this.

In the file: formatting.php line 153 (found under /your/host/wordpressdir/wp-content/plugins/contact-form-7/includes )
comment out the original line like so (in case you want to revert the changes):
//$result = preg_match( '/^[+]?[0-9() -]*$/', $tel );

and add the following:
$pattrn = array( " ", "\r", "\n", "-", ".", "(", ")", "_", "'", "`", "+");
$newphone = str_replace( $pattrn , '' , $tel );
if( strlen( $newphone ) == 10 && preg_match( '/^[0-9]*$/', $newphone )) $result = true;
else $result = false;

Now the user must enter 10 digits no matter if the formatting is +(123) 123 - 1231 with or without spacing and brackets and even underscore or plus sign.

In case anyone is wondering why this is required is because sometimes users might omit their area code (the first three digits) and it becomes very hard to guess what it might be to dial it.

I'm sure this part can be improved because the digits are hard coded and there might be other countries that require more or less so if this was setup from admin and input it there but I don't know how to do this yet and don't require so if someone wants to improve or any feedback let me know in the comments below.

Also check out my previous article if you need to send the data to a database of your own here.

Thanks for visiting.

No comments: