Restrict Memberstack Signup to Custom Domain

To restrict company email domain for Memberstack sign up.

Memberstack offers two options for this:

  1. Restricting access based on a plan, which doesn’t work for default Memberstack plans - Link

  2. Blocking specific email providers, which isn’t practical due to the vast number of email services available - link

The code snippet below reverse the Option 2 logic by allowing you to list the somain you want instead of restricting all other email domain as the list is endless.

NOTE: Replace yourdomain.com with your actual domain.

<script>
// Select the email input element
const emailInput = document.querySelector('input[data-ms-member="email"]');

// Add event listener to the email input field for change event
emailInput.addEventListener('change', (event) => {
  // Get the entered email and extract its domain
  const email = emailInput.value;
  const emailDomain = email.substring(email.lastIndexOf("@") + 1);

  // Allow only emails with the domain 'yourdomain.com'
  if (emailDomain !== 'yourdomain.com') {
    emailInput.setCustomValidity("Only emails ending with 'yourdomain.com' are allowed.");
    emailInput.reportValidity();
    event.preventDefault(); // Prevent form submission
  } else {
    emailInput.setCustomValidity("");
  }
});

// Add event listener to the form submit button
document.querySelector('form').addEventListener('submit', (event) => {
  if (!emailInput.checkValidity()) {
    emailInput.reportValidity();
    event.preventDefault();
  }
});
</script>
2 Likes

This is great thanks for sharing @Michael_Ola

You are welcome @Pablo_Cortes