Title: [Plugin: Contact Form 7] Custom field validation code
Last modified: August 20, 2016

---

# [Plugin: Contact Form 7] Custom field validation code

 *  Resolved [fusionx22](https://wordpress.org/support/users/fusionx22/)
 * (@fusionx22)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/)
 * Hello,
 * I have a client that needs some custom validation code added to their forms.
 * They require a specific ID number, and the number needs to be run through a LUHN
   algorithm (mod 10) to check for validity – similar to verifying a credit card
   number.
 * Is there any way to hook into the process and run a custom validation routine,
   or would I need to hack up the text.php file and add my own tag definitions, 
   etc? I really don’t want to modify plug-in code.
 * Thanks in advance!
 * –jeff
 * [http://wordpress.org/extend/plugins/contact-form-7/](http://wordpress.org/extend/plugins/contact-form-7/)

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2188716)
 * You don’t need t edit the text.php file directly, but looking into the function`
   wpcf7_text_validation_filter()` defined in the text.php would help you learn 
   how you can customize validation in your own plugin or functions.php in your 
   theme.
 * This is a sample code:
 *     ```
       add_filter( 'wpcf7_validate_text', 'your_validation_filter_func', 10, 2 );
       add_filter( 'wpcf7_validate_text*', 'your_validation_filter_func', 10, 2 );
   
       function your_validation_filter_func( $result, $tag ) {
       	$type = $tag['type'];
       	$name = $tag['name'];
   
       	if ( 'your-id-number-field' == $name ) {
       		$the_value = $_POST[$name];
   
       		if ( is_not_valid_against_your_validation( $the_value ) ) {
       			$result['valid'] = false;
       			$result['reason'][$name] = "Error message here";
       		}
       	}
   
       	return $result;
       }
       ```
   
 *  Thread Starter [fusionx22](https://wordpress.org/support/users/fusionx22/)
 * (@fusionx22)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2188917)
 * Thank you very much for this.
 * The one thing that stumps me, is I would like this to work on more than one field
   on the site – there are several forms that require this validation.
 * this line:
 * `if ( 'your-id-number-field' == $name ) {`
 * makes me think I would need to define a new tag that is specific to my ID fields.
   Can that also be done in a new plug-in, to have it available in the Contact Form
   7 plugin? I would think hacking the Contact Form 7 plug-in would be necessary.
 * It would be great if CF7 had user defined tags 🙂
 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2188921)
 * Yes. Add this into your plugin:
 * `wpcf7_add_shortcode( 'yourid', 'wpcf7_text_shortcode_handler', true );`
 * Then, use a tag like this inside the form:
 * `[yourid your-id-number-field]`
 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2188923)
 * In above case,
 *     ```
       $type = $tag['type'];
       $name = $tag['name'];
       ```
   
 * $tag[‘type’] has “yourid” and $tag[‘name’] has “your-id-number-field”. So, your_validation_filter_func()
   should be:
 *     ```
       function your_validation_filter_func( $result, $tag ) {
       	$type = $tag['type'];
       	$name = $tag['name'];
   
       	if ( 'yourid' == $type ) { // <--- this line changed
       		$the_value = $_POST[$name];
   
       		if ( is_not_valid_against_your_validation( $the_value ) ) {
       			$result['valid'] = false;
       			$result['reason'][$name] = "Error message here";
       		}
       	}
   
       	return $result;
       }
       ```
   
 *  Thread Starter [fusionx22](https://wordpress.org/support/users/fusionx22/)
 * (@fusionx22)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2188925)
 * Takayuki-San, domo arigato gozaimasu!
 *  [Karesansui](https://wordpress.org/support/users/karesansui/)
 * (@karesansui)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2189047)
 * Hi Jeff, glad you understood. I also need this but I am still not sure if I understand.
 * Could you place the place the code (part of) you used with the variable and tag-
   names you used ? This may just help me enough to understand the application.
 * As an example, I have two dates and need to check if the start date is before
   the end date. If not issue a message.
 * Thanks,
 *  [KZeni](https://wordpress.org/support/users/kzeni/)
 * (@kzeni)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2189107)
 * I can’t thank you enough for making this plugin as flexible as it is while keeping
   the high-level aspects as simple as they are.
 * I’ve just created a custom verification/validation function that prevents free
   email providers to be used for the email field, and I thought I’d share. BTW,
   this is a fully working example, but you can repurpose/modify it to do anything
   else.
 *     ```
       // Add custom validation for CF7 form fields
       function is_company_email($email){ // Check against list of common public email providers & return true if the email provided *doesn't* match one of them
       	if(
       		preg_match('/@gmail.com/i', $email) ||
       		preg_match('/@hotmail.com/i', $email) ||
       		preg_match('/@live.com/i', $email) ||
       		preg_match('/@msn.com/i', $email) ||
       		preg_match('/@aol.com/i', $email) ||
       		preg_match('/@yahoo.com/i', $email) ||
       		preg_match('/@inbox.com/i', $email) ||
       		preg_match('/@gmx.com/i', $email) ||
       		preg_match('/@me.com/i', $email)
       	){
       		return false; // It's a publicly available email address
       	}else{
       		return true; // It's probably a company email address
       	}
       }
       function custom_email_validation_filter($result,$tag){
       	$type = $tag['type'];
       	$name = $tag['name'];
       	if($name == 'company-email'){ // Only apply to fields with the form field name of "company-email"
       		$the_value = $_POST[$name];
       		if(!is_company_email($the_value)){ // Isn't a company email address (it matched the list of free email providers)
       			$result['valid'] = false;
       			$result['reason'][$name] = 'You need to provide an email address that isn\'t hosted by a free provider.<br />Please contact us directly if this isn\'t possible.';
       		}
       	}
       	return $result;
       }
       add_filter('wpcf7_validate_email','custom_email_validation_filter', 10, 2); // Email field
       add_filter('wpcf7_validate_email*', 'custom_email_validation_filter', 10, 2); // Req. Email field
       ```
   
 * Thanks again, and I hope others might find this helpful.
 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2189108)
 * Thanks, KZeni. I think this is a good example of custom validation.
 *  [Joacim](https://wordpress.org/support/users/joacimjoacimnilssonse/)
 * (@joacimjoacimnilssonse)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2189127)
 * Can someone give me a step-to-step guide for this? Or is it impossible to understand
   if you’re not a good programmer?
 * – Thanks
 *  [Joacim](https://wordpress.org/support/users/joacimjoacimnilssonse/)
 * (@joacimjoacimnilssonse)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2189128)
 * I have found this. Could i implement it somehow?
 * It’s for Swedish use
 * Thanks
 *     ```
       function personnummer(nr){
       	this.valid=false;
       	if(!nr.match(/^(\d{2})(\d{2})(\d{2})\-(\d{4})$/)){ return false; }
       	this.now=new Date(); this.nowFullYear=this.now.getFullYear()+""; this.nowCentury=this.nowFullYear.substring(0,2); this.nowShortYear=this.nowFullYear.substring(2,4);
       	this.year=RegExp.$1; this.month=RegExp.$2; this.day=RegExp.$3; this.controldigits=RegExp.$4;
       	this.fullYear=(this.year*1<=this.nowShortYear*1)?(this.nowCentury+this.year)*1:((this.nowCentury*1-1)+this.year)*1;
       	var months = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
       	if(this.fullYear%400==0||this.fullYear%4==0&&this.fullYear%100!=0){ months[1]=29; }
       	if(this.month*1<1||this.month*1>12||this.day*1<1||this.day*1>months[this.month*1-1]){ return false; }
       	this.alldigits=this.year+this.month+this.day+this.controldigits;
       	var nn="";
       	for(var n=0;n<this.alldigits.length;n++){ nn+=((((n+1)%2)+1)*this.alldigits.substring(n,n+1)); }
       	this.checksum=0;
       	for(var n=0;n<nn.length;n++){ this.checksum+=nn.substring(n,n+1)*1; }
       	this.valid=(this.checksum%10==0)?true:false;
       	this.sex=parseInt(this.controldigits.substring(2,3))%2;
       }
       </script>
   
       <form onsubmit="if(new personnummer(this.pnr.value).valid){ alert('A valid social security number'); } else { alert('No valid social security number'); }; return false;">
       Personnummer: <input type="text" name="pnr"> <input type="submit" value="Check">
       </form>
       ```
   
 *  [Dnesscarkey](https://wordpress.org/support/users/dnesscarkey/)
 * (@dnesscarkey)
 * [14 years ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2189144)
 * This plugin will help to add more validation rules in contact form 7.
    [http://wordpress.org/extend/plugins/jquery-validation-for-contact-form-7/](http://wordpress.org/extend/plugins/jquery-validation-for-contact-form-7/)

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘[Plugin: Contact Form 7] Custom field validation code’ is closed to new
replies.

 * ![](https://ps.w.org/contact-form-7/assets/icon.svg?rev=2339255)
 * [Contact Form 7](https://wordpress.org/plugins/contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7/reviews/)

 * 11 replies
 * 6 participants
 * Last reply from: [Dnesscarkey](https://wordpress.org/support/users/dnesscarkey/)
 * Last activity: [14 years ago](https://wordpress.org/support/topic/plugin-contact-form-7-custom-field-validation-code/#post-2189144)
 * Status: resolved