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

classUserRegisterForm(ModelForm):

role=forms.IntegerField()

check_password= forms.CharField(required=True) #用户的密码验证

defclean(self):

cleaned_data=super(UserRegisterForm, self).clean()#检查用户的唯一性

if 'username' incleaned_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' incleaned_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' incleaned_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' incleaned_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' incleaned_data:

mobile= cleaned_data['mobile']ifmobile.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"请输入正确的邮箱地址,例如:xxxxxx@163.com"

#self._errors = self.error_class([msg])

# #del cleaned_data['email']

returncleaned_datadef __init__(self, *args, **kwargs):

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

model=User

fields= ('username', 'password', 'full_name', 'email', 'mobile')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值