Daniel
Daniel

Reputation: 1

Email Verification Check

I want to have an email check in my email form field so that if the user's email doesn't end with '@aun.edu.ng' it will through an error message.

forms.py

class EmployeeRegistrationForm(forms.ModelForm):
    first_name = forms.CharField(label='First Name')
    last_name = forms.CharField(label='Last Name')
    cv = forms.FileField(label='CV')
    skills_or_trainings = forms.SlugField(label='Skills or Trainings', required=False, widget=forms.Textarea)
    class Meta:
        model = Student
        fields = ['first_name', 'last_name', 'gender', 'level', 'school', 'major', 'cv', 'skills_or_trainings' ]



class EmployerRegistrationForm(forms.ModelForm):
    company_name = forms.CharField(label="Company Name")
    company_address = forms.CharField(label="Company Address")
    website = forms.CharField()

    class Meta:
        model = Employer
        fields = ['company_name', 'company_address', 'website']

views.py

def student_register(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        student_form = EmployeeRegistrationForm(request.POST)

        if form.is_valid() and student_form.is_valid():
            user = form.save(commit=False)
            user.is_student = True
            user.save()

            student = student_form.save(commit=False)
            student.user = user
            student.save()
            return redirect('login')
    else:
        form = UserRegistrationForm()
        student_form = EmployeeRegistrationForm()
    
    context = {'form': form, 'student_form': student_form}
    return render (request, 'accounts/employee/register.html', context)

def employer_register(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        employer_form = EmployerRegistrationForm(request.POST)

        if form.is_valid() and employer_form.is_valid():
            user = form.save(commit=False)
            user.is_employer = True
            user.save()

            employer = employer_form.save(commit=False)
            employer.user = user
            employer.save()
            return redirect('login')
    else:
        form = UserRegistrationForm()
        employer_form = EmployerRegistrationForm()
    
    context = {'form': form, 'employer_form': employer_form}
    return render (request, 'accounts/employer/register.html', context)

Please how do I go about it so that I will be able to through a Validation Error message when the user puts in an email that doesn't end with '@aun.edu.ng' Thank you

Upvotes: 0

Views: 58

Answers (2)

Lewis
Lewis

Reputation: 2798

To raise validation error for the employer you can do this by overriding the clean() method of the EmployerRegistrationForm form.

from django.form import ValidationError

class EmployerRegistrationForm(forms.ModelForm):
    company_name = forms.CharField(label="Company Name")
    company_address = forms.CharField(label="Company Address")
    website = forms.CharField()

    class Meta:
        model = Employer
        fields = ['company_name', 'company_address', 'website']
    
    def clean(self):
        cleaned_data = super().clean()
        if cleaned_data.get('company_address').endswith('@aun.edu.ng'):
            raise ValidationError('This email is not applicable')
        return cleaned_data 

Upvotes: 1

The Otterlord
The Otterlord

Reputation: 2185

See this answer for a regular expression that matches email addresses. You can use this in Python with the regular expression module. If the email is invalid, just send them back to the login with a message saying the email address is invalid.

import re

email_re = """(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"""

if re.match(email_re, "[email protected]"):
    print("Match found")
else:
    print("Invalid email address")

Please note that valid email addresses include unregistered email addresses and the only way to verify an email address completely is to send them an email with a verification link. This link would then allow them to "unlock" the account created.

Upvotes: 0

Related Questions