1.自定义正则表达式
password = django_fields.RegexField( '^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$\%^&*()])[0-9a-zA-Z!@#$\%^&*()]{8,32}$', min_length=12, max_length=32, error_messages={'required': '密码不能为空.', 'invalid': '密码必须包含数字,字母、特殊字符', 'min_length': "密码长度不能小于8个字符", 'max_length': "密码长度不能大于32个字符"} )
2.clean_%s
def clean_email(self): if 'email' in self.cleaned_data: if models.UserInfo.objects.filter(email=self.cleaned_data['email']) is None: raise ValidationError(message='邮箱已经存在')
在类中自定义以clean_%s的方法(以上是验证邮箱是否已经存在)
3.clean
def clean(self): if 'password' in self.cleaned_data and 'confirm_pwd' in self.cleaned_data: v1 = self.cleaned_data['password'] v2 = self.cleaned_data['confirm_pwd'] print(v1,v2) if v1 == v2: pass else: from django.core.exceptions import ValidationError raise ValidationError('密码输入不一致') else: print('格式错误')
联合验证,错误信息在form.errors["__all__"],templates中{{ result.form.non_field_errors.0}}