Block free email providers in a contact form
Sometimes site owners want the email addresses of official company representatives who register or send requests on their sites, not personal Gmail or Yahoo addresses. In this case the email field of the contact form has to be checked.
Problem: Do not allow addresses from free email providers in a contact form.
Solution: For simplicity, let’s split the solution into a few steps: a list of free domains in a JSON file, a jQuery check while typing and on submit, and the same check on the server.
Step 1. The list of free email domains
Find a list of free email providers and create a JSON file from it. I took the list from the gist in the sources (a few thousand domains):
[
{"domain":"1033edge.com"},
{"domain":"11mail.com"},
{"domain":"123.com"},
...
{"domain":"zydecofan.com"},
{"domain":"zzn.com"},
{"domain":"zzom.co.uk"}
]
Step 2. Pass the file URL to JavaScript
Put free_email_vendors.json in the theme folder with the JS files and pass the theme URL to the script in functions.php:
<?php
add_action( 'wp_enqueue_scripts', 'enqueue_theme_scripts' );
function enqueue_theme_scripts() {
wp_enqueue_script( 'main-script', get_template_directory_uri() . '/js/jquery.main.js', array( 'jquery' ), null, true );
wp_localize_script( 'main-script', 'base', array(
'template_url' => get_template_directory_uri(),
) );
}
Step 3. Check the email in the form
The script loads the list once, checks the domain while the user types, and checks it again on submit:
( function ( $ ) {
var form = $( '#form' );
var emailField = form.find( '[type=email]' );
if ( ! form.length || ! emailField.length ) {
return;
}
var freeEmailVendors = [];
var message = '<span class="email-warning-message">Sorry, you need to use a company email address</span>';
// Get the list of free email vendors
$.getJSON( base.template_url + '/js/free_email_vendors.json', function ( data ) {
$.each( data, function ( key, val ) {
freeEmailVendors.push( val.domain );
} );
} );
function isFreeEmail( email ) {
var domain = email.split( '@' )[1];
return domain ? $.inArray( domain.toLowerCase(), freeEmailVendors ) !== -1 : false;
}
function check() {
var bad = isFreeEmail( emailField.val() );
form.find( '.email-warning-message' ).remove();
if ( bad ) {
emailField.after( message );
}
form.find( '[type=submit]' ).prop( 'disabled', bad );
return ! bad;
}
// While typing
emailField.on( 'input', check );
// On submit
form.on( 'submit', function ( event ) {
if ( ! check() ) {
event.preventDefault();
}
} );
} )( jQuery );
An example of the form:
<form id="form" class="form" name="form">
<div class="input_container">
<input type="email" name="email" value="" placeholder="Your Email" class="email" autocomplete="off" required>
</div>
<input type="submit" name="submit_button" value="Submit" class="submit">
</form>
Step 4. Check on the server too
JavaScript can be turned off or bypassed, so the form handler must check the domain as well. A helper that reads the same JSON file:
<?php
function is_free_email( $email ) {
static $domains = null;
if ( null === $domains ) {
$list = json_decode( file_get_contents( get_template_directory() . '/js/free_email_vendors.json' ), true );
$domains = wp_list_pluck( (array) $list, 'domain' );
}
$domain = strtolower( substr( strrchr( $email, '@' ), 1 ) );
return in_array( $domain, $domains, true );
}
If the form is made with Contact Form 7, connect it to the validation of the email field:
<?php
add_filter( 'wpcf7_validate_email*', 'cf7_block_free_email', 20, 2 );
function cf7_block_free_email( $result, $tag ) {
$value = isset( $_POST[ $tag->name ] ) ? sanitize_email( wp_unslash( $_POST[ $tag->name ] ) ) : '';
if ( $value && is_free_email( $value ) ) {
$result->invalidate( $tag, 'Sorry, you need to use a company email address' );
}
return $result;
}
Sources: