Login

New forms signup validation

Author:
Batiste
Posted:
April 19, 2007
Language:
Python
Version:
.96
Tags:
newforms email clean signup password
Score:
9 (after 9 ratings)

This snippets provide username availability, double email and password validation. You can use it this way :

f = SignupForm(request.POST)
f.is_valid()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class UserField(forms.CharField):
    def clean(self, value):
        super(UserField, self).clean(value)
        try:
            User.objects.get(username=value)
            raise forms.ValidationError("Someone is already using this username. Please pick an other.")
        except User.DoesNotExist:
            return value

class SignupForm(forms.Form):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)
    username = UserField(max_length=30)
    password = forms.CharField(widget=forms.PasswordInput())
    password2 = forms.CharField(widget=forms.PasswordInput(), label="Repeat your password")
    email = forms.EmailField()
    email2 = forms.EmailField(label="Repeat your email")
    
    def clean_email(self):
        if self.data['email'] != self.data['email2']:
            raise forms.ValidationError('Emails are not the same')
        return self.data['email']

    def clean_password(self):
        if self.data['password'] != self.data['password2']:
            raise forms.ValidationError('Passwords are not the same')
        return self.data['password']
    
    def clean(self,*args, **kwargs):
        self.clean_email()
        self.clean_password()
        return super(SignupForm, self).clean(*args, **kwargs)

More like this

  1. use oldforms validators in newforms forms by garywilson 8 years, 1 month ago
  2. Email validation by mulamvenky 2 years ago
  3. MySQL django password function by mcosta 5 years, 12 months ago
  4. Password Validation - Require Letters and Numbers - no regex by watchedman 3 years, 8 months ago
  5. create_model_instances management command by Lacrymology 2 years, 4 months ago

Comments

rogelio (on May 5, 2008):

Thank you very much for the contribution, i'm using this snippet as a base form my signup form now =)

#

artur_mwaigaryan (on January 18, 2012):

very elegant solution :-)

#

sbhuvanas (on February 18, 2015):

thank you very much !!! it worked for me for password check

#

Please login first before commenting.