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"请输入正确的邮箱地址,例如:xxxxxx@163.com"
# 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. Base64 报错 的解决办法 (Base-64 字符数组或字符串的长度无效。, 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符。)

    Base64 报错 的解决办法, 报错如下:1. FormatException: The input is not a valid Base-64 string as it contains a n ...

  2. SQL判断某列中是否包含中文字符或者英文字符

    SQL判断某列中是否包含中文字符或者英文字符   [sql]  select * from 表名 where 某列 like '%[吖-座]%'     select * from 表名 where ...

  3. FromBase64String 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符

    js前台: <input id="upload_img_input" v-on:change="onFileChange" type="file ...

  4. 小程序码B接口生成出错:场景内容包含非法字符

    由于包含了非法字符,微信返回的字节不超过100字符,但是没有包含提示内容,因此很难识别发现问题所在

  5. SQL判断某列中是否包含中文字符、英文字符、纯数字 (转)

    一.包含中文字符 select * from 表名 where 列名 like '%[吖-座]%' 二.包含英文字符 select * from 表名 where 列名 like '%[a-z]%' ...

  6. java判断A字符串中是否包含B字符

    java.lang.String类提供的方法 public boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true. 例如: ...

  7. [leetcode]242. Valid Anagram判断两个字符串是不是包含相同字符的重排列

    /* 思路是判断26个字符在两个字符串中出现的次数是不是都一样,如果一样就返回true. 记住这个方法 */ if (s.length()!=t.length()) return false; int ...

  8. 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符

    正常URL: http://localhost:16990/GoodsOrder/OrderRevoke.aspx??6G5lFi6xuoiLDhfOOOIkBYwy8RGpkfuza2gLlJrlT ...

  9. 判断数组(array)中是否包含某个字符(contains)

    $a="a","","b" $a -contains "a" 返回 $true $a -notcontains &quo ...

随机推荐

  1. RichtextBox去除闪烁光标

    http://files.cnblogs.com/xe2011/CustomRichTextBox_HideCaret.rar richTextBox能高亮选择,光标仍在,没有光标闪烁 把重RichT ...

  2. SCTP 关联的建立和终止

    与TCP一样,SCTP也是面向连接的,因而也有关联的建立与终止的握手过程.不过SCTP的握手过程不同于TCP. 四路握手 建立一个SCTP关联的时候会发生下述情形(类似于TCP). (1)服务器必须准 ...

  3. Intra-cluster Replication in Apache Kafka--reference

    Kafka is a distributed publish-subscribe messaging system. It was originally developed at LinkedIn a ...

  4. telnet的使用

    1.要打开 telnet 不是内部或外部 命令 解决方案: 程序添加删除功能,添加即可 或法二 C:\WINDOWS\system32\telnet.exe (或用C:\WINDOWS\system3 ...

  5. thinking in java 读书笔记 --- overriding private method

    一个对象可以被用作它自身的类型或者是它的基类类型.当用作它的基类类型时叫做upcasting,如下所示 BaseClass ref = new DerivedClass()  //upcasting ...

  6. 配置toad进行导入导出数据库操作

    最近需要对oracle进行导入导出操作,于是研究使用toad进行导入导出oracle数据库操作,现稍作记录. oracle的导入导出不管是否使用toad还是pl/sql工具进行操作,实际上最终都是通过 ...

  7. ASP.NET MVC 第六回 过滤器Filter

    在Asp.netMvc中当你有以下及类似以下需求时你可以使用Filter功能 判断登录与否或用户权限 决策输出缓存 防盗链 防蜘蛛 本地化与国际化设置 实现动态Action Filter是一种声明式编 ...

  8. couchbase failover 集群故障自动转移方案研究!

    最近迷上Couchbase了,现在所有的站点全部试用Couchbase进行缓存及持久化,这样以来貌似风险比较大啊,缓存服务器挂了就完了. 看到有讲到Couchbase的集群方案很简单,于是照着教程做了 ...

  9. Java-struts2 之值栈问题

    这里是根据一个小项目,将数据库的值查出来,然后在页面前台进行遍历的方法 放入值的几种方式: Struts2的三种存值取值的方式 值栈: 栈上下文: ActionContext: package com ...

  10. 微信公众平台开发(一)——接入指南(asp.net)

    第一步:申请消息接口 在公众平台网站的高级功能 – 开发模式页,点击“成为开发者”按钮,填写URL和Token,其中URL是开发者用来接收微信服务器数据的接口URL.Token可由开发者任意填写,用作 ...