Skip to content

Instantly share code, notes, and snippets.

@stevejalim
Created April 24, 2012 11:33
Show Gist options
  • Select an option

  • Save stevejalim/2478912 to your computer and use it in GitHub Desktop.

Select an option

Save stevejalim/2478912 to your computer and use it in GitHub Desktop.
Django StrongPasswordField
import re
from django import forms
def is_password_strong_enough(value):
"""
"All passwords should be:
10+ characters long
contain at least one upper-case character
contain at least one lower-case character
contain at least one numeric digit
^.*
(?=.{10,}) # at least 10 chars
(?=.*\d) # at least 1 numeric digit
(?=.*[A-Z]) #at least one uppercase letter
(?=.*[a-z]) #at least one lowercase letter
.*$
"""
hit = re.match("^.*(?=.{6,})(?=.*\d)(?=.*[A-Z]).*$", value)
return bool(hit)
class StrongPasswordField(forms.CharField):
"""
Checks that a password meets the minimum criteria defined in check_password_security()
"""
widget = forms.widgets.PasswordInput
def validate(self, value):
# Use the parent's handling of required fields, etc.
super(forms.CharField, self).validate(value)
#then our own validator
if value and not is_password_strong_enough(value):
raise forms.ValidationError("Passwords must be at least 10 characters long with at least one upper-case letter, at least one lower-case letter and at least one number.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment