How to Validation Business Email in Contact Form 7 WordPress

How to Validation Business Email in Contact Form 7 WordPress

Today we will show you how to validate or accept only business email in contact form 7 WordPress.

Below i will show you step step how can we check entered email id is business email or not on submit

Step 1:

Here we create a function is_business_email() inside function.php file of theme in this function we define free email id domains like gmail, yahoo etc.

function is_business_email($email){
	if(preg_match('/@hotmail.com/i', $email) ||
		preg_match('/@gmail.com/i', $email) ||
		preg_match('/@yahoo.co/i', $email) ||
		preg_match('/@yahoo.com/i', $email) ||
		preg_match('/@mailinator.com/i', $email) ||
		preg_match('/@gmail.co.in/i', $email) ||
		preg_match('/@aol.com/i', $email) ||
		preg_match('/@yandex.com/i', $email) ||
		preg_match('/@msn.com/i', $email) ||
		preg_match('/@gawab.com/i', $email) ||
		preg_match('/@inbox.com/i', $email) ||
		preg_match('/@gmx.com/i', $email) ||
		preg_match('/@rediffmail.com/i', $email) ||
		preg_match('/@in.com/i', $email) ||
		preg_match('/@live.com/i', $email) ||
		preg_match('/@hotmail.co.uk/i', $email) ||
		preg_match('/@hotmail.fr/i', $email) ||
		preg_match('/@yahoo.fr/i', $email) ||
		preg_match('/@wanadoo.fr/i', $email) ||
		preg_match('/@wanadoo.fr/i', $email) ||
		preg_match('/@comcast.net/i', $email) ||
		preg_match('/@yahoo.co.uk/i', $email) ||
		preg_match('/@yahoo.com.br/i', $email) ||
		preg_match('/@yahoo.co.in/i', $email) ||
		preg_match('/@rediffmail.com/i', $email) ||
		preg_match('/@free.fr/i', $email) ||
		preg_match('/@gmx.de/i', $email) ||
		preg_match('/@gmx.de/i', $email) ||
		preg_match('/@yandex.ru/i', $email) ||
		preg_match('/@ymail.com/i', $email) ||
		preg_match('/@libero.it/i', $email) ||
		preg_match('/@outlook.com/i', $email) ||
		preg_match('/@uol.com.br/i', $email) ||
		preg_match('/@bol.com.br/i', $email) ||
		preg_match('/@mail.ru/i', $email) ||
		preg_match('/@cox.net/i', $email) ||
		preg_match('/@hotmail.it/i', $email) ||
		preg_match('/@sbcglobal.net/i', $email) ||
		preg_match('/@sfr.fr/i', $email) ||
		preg_match('/@live.fr/i', $email) ||
		preg_match('/@verizon.net/i', $email) ||
		preg_match('/@live.co.uk/i', $email) ||
		preg_match('/@googlemail.com/i', $email) ||
		preg_match('/@yahoo.es/i', $email) ||
		preg_match('/@ig.com.br/i', $email) ||
		preg_match('/@live.nl/i', $email) ||
		preg_match('/@bigpond.com/i', $email) ||
		preg_match('/@terra.com.br/i', $email) ||
		preg_match('/@yahoo.it/i', $email) ||
		preg_match('/@neuf.fr/i', $email) ||
		preg_match('/@yahoo.de/i', $email) ||
		preg_match('/@live.com/i', $email) ||
		preg_match('/@yahoo.de/i', $email) ||
		preg_match('/@rocketmail.com/i', $email) ||
		preg_match('/@att.net/i', $email) ||
		preg_match('/@laposte.net/i', $email) ||
		preg_match('/@facebook.com/i', $email) ||
		preg_match('/@bellsouth.net/i', $email) ||
		preg_match('/@yahoo.in/i', $email) ||
		preg_match('/@hotmail.es/i', $email) ||
		preg_match('/@charter.net/i', $email) ||
		preg_match('/@yahoo.ca/i', $email) ||
		preg_match('/@yahoo.com.au/i', $email) ||
		preg_match('/@rambler.ru/i', $email) ||
		preg_match('/@hotmail.de/i', $email) ||
		preg_match('/@tiscali.it/i', $email) ||
		preg_match('/@shaw.ca/i', $email) ||
		preg_match('/@yahoo.co.jp/i', $email) ||
		preg_match('/@sky.com/i', $email) ||
		preg_match('/@earthlink.net/i', $email) ||
		preg_match('/@optonline.net/i', $email) ||
		preg_match('/@freenet.de/i', $email) ||
		preg_match('/@t-online.de/i', $email) ||
		preg_match('/@aliceadsl.fr/i', $email) ||
		preg_match('/@virgilio.it/i', $email) ||
		preg_match('/@home.nl/i', $email) ||
		preg_match('/@qq.com/i', $email) ||
		preg_match('/@telenet.be/i', $email) ||
		preg_match('/@me.com/i', $email) ||
		preg_match('/@yahoo.com.ar/i', $email) ||
		preg_match('/@tiscali.co.uk/i', $email) ||
		preg_match('/@yahoo.com.mx/i', $email) ||
		preg_match('/@gmx.net/i', $email) ||
		preg_match('/@mail.com/i', $email) ||
		preg_match('/@planet.nl/i', $email) ||
		preg_match('/@tin.it/i', $email) ||
		preg_match('/@live.it/i', $email) ||
		preg_match('/@ntlworld.com/i', $email) ||
		preg_match('/@arcor.de/i', $email) ||
		preg_match('/@yahoo.co.id/i', $email) ||
		preg_match('/@frontiernet.net/i', $email) ||
		preg_match('/@hetnet.nl/i', $email) ||
		preg_match('/@live.com.au/i', $email) ||
		preg_match('/@yahoo.com.sg/i', $email) ||
		preg_match('/@zonnet.nl/i', $email) ||
		preg_match('/@club-internet.fr/i', $email) ||
		preg_match('/@juno.com/i', $email) ||
		preg_match('/@optusnet.com.au/i', $email) ||
		preg_match('/@blueyonder.co.uk/i', $email) ||
		preg_match('/@bluewin.ch/i', $email) ||
		preg_match('/@skynet.be/i', $email) ||
		preg_match('/@sympatico.ca/i', $email) ||
		preg_match('/@windstream.net/i', $email) ||
		preg_match('/@mac.com/i', $email) ||
		preg_match('/@centurytel.net/i', $email) ||
		preg_match('/@chello.nl/i', $email) ||
		preg_match('/@live.ca/i', $email) ||
		preg_match('/@aim.com/i', $email) ||
		preg_match('/@bigpond.net.au/i', $email))
	{
		return false; // It’s a publicly available email address
	}
	else{
		return true; // It’s probably a company email address
	}
}

Step 2:

Now we have to create a function name of custom_email_validation_filter() inside theme function.php. This function will be execute on click submit button. Basically this function will be check whether email is business or not.
This function will be call by contact form 7 filter(wpcf7_validate_email).

function custom_email_validation_filter($result, $tag) {
	$tag = new WPCF7_Shortcode( $tag );
	if ( 'your-email' == $tag->name ) {
		$the_value = isset( $_POST['your-email'] ) ? trim( $_POST['your-email'] ) : "";
		if(!is_business_email($the_value)){
			$result->invalidate( $tag, "Please enter a valid business email" );
		}
	}
	return $result;
}
add_filter( 'wpcf7_validate_email', 'custom_email_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_email*', 'custom_email_validation_filter', 10, 2 );
Note: Here we are using email field name(your-email), if you are using different name of email field then don’t forget to change name of filed in function custom_email_validation_filter().

Tags:
How to validation business email in Contact Form 7, Accept only business email Contact Form 7, Custom validation to check business email contact form 7, Block/Limit Email Domains for Contact Form 7 Email Fields, wordpress contact form 7 email validation, contact form 7 acceptance, contact form 7 unique email, contact form 7 conditional fields, contact form 7 required field not working, contact form 7 phone validation, contact form 7 conditional email, contact form 7 your email error.

About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.