Whitelist Specific Email Addresses [RESOLVED]

I’m working on a website for a membership organization. On this site I have a form so users can submit a news article. I don’t want to force our users to remember any passwords to access this form, rather I’d like to add approved emails to a whitelist for the form.

I’ve found plenty of documentation about how to block certain emails/domains, but is it possible to whitelist specific emails and not allow submissions that don’t come from approved emails?

Thank you in advance!

Any validation to block something can generally be flipped to whitelist something.

You can for instance use the gform_field_validation filter in your theme functions.php file or a custom functions plugin to throw a validation error for an email field if the value doesn’t exist on a set list:

add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {

    $whitelisted_emails = array(
            'test@test.com',
            'test2@test.com'
    );

    if ( $result['is_valid'] && $field->get_input_type() === 'email' && ! in_array( $value, $whitelisted_emails ) ) {
        $result['is_valid'] = false;
        $result['message']  = 'Invalid Email';
    }

    return $result;

}, 10, 4 );

The above is a rough example that declares a whitelist of emails in the $whitelisted_emails array and then runs validation on any value entered into any email field against that whitelist of emails. Any email entered that isn’t test@test.com or test2@test.com will throw a validation error.

Another way is to simply add conditional logic to your other form fields based on whether or not the email field has any of the emails you’ve added into your logic.

If email field is not approvedemail@email.com then hide this field.

If email field is not otherapprovedemail@email.com then hide this field.

To reduce the amount of logic you can add your conditions to the submit button or group the other fields of your form inside a page break and apply the logic to just the page field.

Thank you both for your responses! I went with the PHP method as I had multiple forms I wanted to validate from the same list. The above code works great for one form, but if someone wants to use the same list for multiple forms I modified it as follows:

add_filter( 'gform_field_validation_3_3','validate_email', 10, 4);
add_filter( 'gform_field_validation_4_25','validate_email', 10, 4);		   
		   
		   function validate_email ( $result, $value, $form, $field ) {

    $whitelisted_emails = array(
            'test@test.com',
            'test2@test.com'
    );

    if ( $result['is_valid'] && $field->get_input_type() === 'email' && ! in_array( $value, $whitelisted_emails ) ) {
        $result['is_valid'] = false;
        $result['message']  = 'Unapproved Email Address: You must be a member and have your email approved to use this form. If this an error, please contact us.';
    }

    return $result;

} 

That’s a fine way to do it. If you have any other questions, please let us know.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.