Ruc*_*hit 1 python regex django
默认情况下,在 Django 2.0 中,我们使用AUTH_PASSWORD_VALIDATORS选项
那么有没有什么简单的方法可以添加额外的验证器,例如最少 1 个大写字母、1 个符号、1 个数字等?
在 python 中,我可以检查 Using regex
import re
userPass = 'HelloWorld*123'
if re.search('[A-Z]', userPass)!=None and re.search('[0-9]', userPass)!=None and re.search('[^A-Za-z0-9]', userPass)!=None:
print 'Strong Password'
Run Code Online (Sandbox Code Playgroud)
干得好:
class _ValidatorBase:
__slots__ = ('message',)
DEFAULT_MSG = ''
def __init__(self, message=None):
self.message = message if message else self.DEFAULT_MSG
def get_help_text(self):
return self.message
def validate(self, *args, **kwargs):
raise NotImplementedError()
class HasLowerCaseValidator(_ValidatorBase):
__slots__ = ()
DEFAULT_MSG = "The password must contain at least one lowercase character."
def validate(self, password, user=None):
if re.search('[a-z]', password) is None:
raise ValidationError(self.message, code='missing_lower_case')
class HasUpperCaseValidator(_ValidatorBase):
__slots__ = ()
DEFAULT_MSG = "The password must contain at least one uppercase character."
def validate(self, password, user=None):
if re.search('[A-Z]', password) is None:
raise ValidationError(self.message, code='missing_upper_case')
class HasNumberValidator(_ValidatorBase):
__slots__ = ()
DEFAULT_MSG = "The password must contain at least one numeric character."
def validate(self, password, user=None):
if re.search('[0-9]', password) is None:
raise ValidationError(self.message, code='missing_numeric')
class HasSymbolValidator(_ValidatorBase):
__slots__ = ()
DEFAULT_MSG = "The password must contain at least one non-alphanumeric character (symbol)."
def validate(self, password, user=None):
if re.search('[^A-Za-z0-9]', password) is None:
raise ValidationError(self.message, code='missing_symbol')
Run Code Online (Sandbox Code Playgroud)