[Django]-Validate Custom Email Domains with Django-Registration

4👍

You can create your own form in forms.py, you already have some example for :

  • registration with term of service
  • registration with unique mail
  • registration without freeemail

In your case add :

class RegistrationFormEduMail(RegistrationForm):

    good_domains = ['edu']

    def clean_email(self):

        email_domain = self.cleaned_data['email'].split('.')[-1]
        if email_domain not in self.good_domains:
            raise forms.ValidationError(_("Registration using non edu email addresses is prohibited. Please supply a different email address."))
        return self.cleaned_data['email']

Then go in registration/backends/default/init.py and import your form, change the name of the form returned by get_form_class() method to the name of your form (here: RegistrationFormEduMail)

👤ltbesh

Leave a comment