python判断用户注册中用户名是否包含非法字符

class UserRegisterForm(ModelForm):
    role = forms.IntegerField()
    check_password = forms.CharField(required=True)  # 用户的密码验证

    def clean(self):
        cleaned_data = super(UserRegisterForm, self).clean()
        #  检查用户的唯一性
        if 'username' in cleaned_data:
            username = cleaned_data['username']
            if User.objects.filter(username=username).count() > 0:
                msg = u"此用户已被注册!!。"
                self._errors["username"] = self.error_class([msg])

                del cleaned_data["username"]

            if not re.search(u'^[_a-zA-Z0-9\u4e00-\u9fa5]+$', username):
                msg = u"用户名不可以包含非法字符(!,@,#,$,%...)"
                self._errors['username'] = self.error_class([msg])
                del cleaned_data['username']

        # if "username" in cleaned_data:
        #     user_name = cleaned_data['username']
        #
        #     # 判断用户名是否包含非法字符
        #     if not re.search(u'^[_a-zA-Z0-9\u4e00-\u9fa5]+$', user_name):
        #         msg = u"用户名包含了非法字符"
        #         self._errors['user_name'] = self.error_class([msg])
        #         del cleaned_data['username']

        if 'password' in cleaned_data and 'check_password' in cleaned_data:
            password = cleaned_data['password']
            check_password = cleaned_data['check_password']

            if len(password) < 6 or len(check_password) < 6:
                msg = u'密码太简单!(建议密码长度为6-18位!)'

                self._errors['password'] = self.error_class([msg])
                self._errors['check_password'] = self.error_class([msg])

                del cleaned_data['password']
                del cleaned_data['check_password']

        if 'password' in cleaned_data and 'check_password' in cleaned_data:
            password = cleaned_data['password']
            check_password = cleaned_data['check_password']
            if password != check_password:
                msg = u'两次输入的密码不配配!'

                self._errors['password'] = self.error_class([msg])
                self._errors['check_password'] = self.error_class([msg])

                del cleaned_data['password']
                del cleaned_data['check_password']

        if 'password' in cleaned_data and 'check_password' in cleaned_data:
            password = cleaned_data['password']
            check_password = cleaned_data['check_password']
            if password is u'' or check_password is u'':
                msg = u'密码不可以为空!'
                self._errors['password'] = self.error_class([msg])
                self._errors['check_password'] = self.error_class([msg])

                del cleaned_data['password']
                del cleaned_data['check_password']

        if 'mobile' in cleaned_data:
            mobile = cleaned_data['mobile']
            if mobile.isdigit():
                if len(mobile) != 11:
                    msg = u"请输入正确的手机号码!例如:15925087562"
                    self._errors['mobile'] = self.error_class([msg])

                    del cleaned_data['mobile']

        # 用户注册邮箱验证试用Django的认证方式
        # if 'email' in cleaned_data:
        #     email = cleaned_data['email']
        #     if len(email) > 7:
        #         if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) == None:
        #             msg = u"请输入正确的邮箱地址,例如:[email protected]"
        #             self._errors = self.error_class([msg])
        #
        #             del cleaned_data['email']

        return cleaned_data

    def __init__(self, *args, **kwargs):
        super(UserRegisterForm, self).__init__(*args, **kwargs)

    class Meta:
        model = User
        fields = ('username', 'password', 'full_name', 'email', 'mobile')

 python判断用户注册中用户名是否包含非法字符_第1张图片

你可能感兴趣的:(python)