-->

Validate Custom Email Domains with Django-Registra

2019-07-16 11:45发布

问题:

I saw a version of JQuery EDU validation here but I'd love to use django-registraion to check a full domain @someschool.edu or @alumni.someschool.edu

Any ideas? Thanks for your help.

回答1:

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)