Email Domain Validator

Exclude a list of invalid domains or include a list of valid domains for your Gravity Form Email fields.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.
Read the Walkthrough

Code

Filename: gw-email-domain-validator.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
 * Gravity Wiz // Gravity Forms // Email Domain Validator
 * https://gravitywiz.com/banlimit-email-domains-for-gravity-form-email-fields/
 *
 * Allows you to exclude a list of invalid domains or include a list of valid domains for your Gravity Form Email fields.
 *
 * Plugin Name:  Gravity Forms - Email Domain Validator
 * Plugin URI:   https://gravitywiz.com/banlimit-email-domains-for-gravity-form-email-fields/
 * Description:  Exclude a list of invalid domains or include a list of valid domains for your Gravity Form Email fields.
 * Author:       Gravity Wiz
 * Version:      0.1
 * Author URI:   https://gravitywiz.com/
 */
class GW_Email_Domain_Validator {

	private $_args;

	function __construct( $args ) {

		$this->_args = wp_parse_args( $args, array(
			'form_id'            => false,
			'field_id'           => false,
			'domains'            => false,
			// translators: placeholder is a domain for emails that are not allowed.
			'validation_message' => __( 'Sorry, <strong>%s</strong> email accounts are not eligible for this form.' ),
			'mode'               => 'ban', // also accepts "limit"
		) );

		// convert field ID to an array for consistency, it can be passed as an array or a single ID
		if ( $this->_args['field_id'] && ! is_array( $this->_args['field_id'] ) ) {
			$this->_args['field_id'] = array( $this->_args['field_id'] );
		}

		$form_filter = $this->_args['form_id'] ? "_{$this->_args['form_id']}" : '';

		add_filter( "gform_validation{$form_filter}", array( $this, 'validate' ) );

	}

	function validate( $validation_result ) {

		$form = $validation_result['form'];

		foreach ( $form['fields'] as &$field ) {

			// if this is not an email field, skip
			if ( RGFormsModel::get_input_type( $field ) != 'email' ) {
				continue;
			}

			// if field ID was passed and current field is not in that array, skip
			if ( $this->_args['field_id'] && ! in_array( $field['id'], $this->_args['field_id'] ) ) {
				continue;
			}

			$page_number = GFFormDisplay::get_source_page( $form['id'] );
			if ( $page_number > 0 && $field->pageNumber != $page_number ) {
				continue;
			}

			if ( GFFormsModel::is_field_hidden( $form, $field, array() ) ) {
				continue;
			}

			$domain = $this->get_email_domain( $field );

			// if domain is valid OR if the email field is empty, skip
			if ( $this->is_domain_valid( $domain ) || empty( $domain ) ) {
				continue;
			}

			$validation_result['is_valid'] = false;
			$field['failed_validation']    = true;
			$field['validation_message']   = sprintf( $this->_args['validation_message'], $domain );

		}

		$validation_result['form'] = $form;
		return $validation_result;
	}

	function get_email_domain( $field ) {
		$email = explode( '@', rgpost( "input_{$field['id']}" ) );
		return trim( rgar( $email, 1 ) );
	}

	function is_domain_valid( $domain ) {

		$mode   = $this->_args['mode'];
		$domain = strtolower( $domain );

		foreach ( $this->_args['domains'] as $_domain ) {

			$_domain = strtolower( $_domain );

			$full_match   = $domain == $_domain;
			$suffix_match = strpos( $_domain, '.' ) === 0 && $this->string_ends_with( $domain, $_domain );
			$has_match    = $full_match || $suffix_match;

			if ( $mode == 'ban' && $has_match ) {
				return false;
			} elseif ( $mode == 'limit' && $has_match ) {
				return true;
			}
		}

		return $mode == 'limit' ? false : true;
	}

	function string_ends_with( $string, $text ) {

		$length      = strlen( $string );
		$text_length = strlen( $text );

		if ( $text_length > $length ) {
			return false;
		}

		return substr_compare( $string, $text, $length - $text_length, $text_length ) === 0;
	}

}

class GWEmailDomainControl extends GW_Email_Domain_Validator { }

# Configuration

new GW_Email_Domain_Validator( array(
	'form_id'            => 326,
	'field_id'           => 1,
	'domains'            => array( 'gmail.com', 'hotmail.com', '.co.uk' ),
	// translators: placeholder is a domain for emails that are not allowed.
	'validation_message' => __( 'Oh no! <strong>%s</strong> email accounts are not eligible for this form.' ),
	'mode'               => 'limit',
) );

Leave a Reply

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

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.