Forbid Free Emails in Your Signup Form

Lisa Fockens Updated by Lisa Fockens

It is possible to prevent visitors from subscribing with a free email (with domains such as gmail.com, hotmail.com, facebook.com, etc). 

To do so, we'll use the Wisepops JS callbacks feature to download a list of free email providers, and ensure that the listed domains are not used in the email field.

Step 1: 

From our editor, create a new JS callback listening for the event Before popup display:

Step 2:

Copy and paste the following snippet in the script area:

// Get list of free email domains
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://wisepops.gitlab.io/free-email-providers/list.txt', true);
xhr.onload = function () {
  if (xhr.status === 200) {
    window.wisepopsFreeEmailDomains = xhr.responseText.split("\n");
  }
};
xhr.send();

When the popup is being displayed, we will load the list of free email providers from a public resource maintained by Wisepops.

The list is loaded asynchronously – meaning that it won't delay the popup.

Step 3:

Create a second callback listening for the Before form submit event.

Step 4:

Copy and paste the following snippet into the callback's script:

// Prevent sign-up with a free email
// This script uses the list fetched by the before-popup-display callback
var isFreeEmail = false;
var emailField = event.target.elements['email'];
var emailParts = emailField.value.split('@');
if (emailParts.length === 2 && window.wisepopsFreeEmailDomains) {
  isFreeEmail = window.wisepopsFreeEmailDomains.indexOf(emailParts[1]) > -1;
}
if (emailField) {
  if (isFreeEmail) {
    emailField.setCustomValidity('Please use a professional email.');
  } else {
    emailField.setCustomValidity('');
  }  
}

Here we will try to match the filled email domain against the previously fetched lists. If a match is found, we will display this error message: "Please use a professional email.” 

And voila! You final config should look like this:

You can learn more about how to write custom validations for your forms here.

Contact