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. Android时间互换代码

    收藏备用. http://www.oschina.net/code/snippet_575610_22694 /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH ...

  2. codeblocks创建和使用静态库(C语言)

    静态库  (扩展名为 .a 或 .lib) 是包含函数的文件,用于在link阶段整合执行程序,动态链接库(扩展名  .dll)是不在link阶段整合进执行程序中的. DLL文件在执行阶段动态调用 下面 ...

  3. cocos2dx 文件处理

    问题1:fopen 在vs下使用fopen进行文件处理,跑通了,但是移植到android源码下时就出现了一大推问题,首先需要理解的是在vs下开发资源是存放在执行文件的相同目录下的,而移植到androi ...

  4. spring+hibernate+jpa+Druid的配置文件,spring整合Druid

    spring+hibernate+jpa+Druid的配置文件 spring+hibernate+jpa+Druid的完整配置 spring+hibernate+jpa+Druid的数据源配置 spr ...

  5. JS判断是否是移动设备进行http链接重定向

    1.问题: 用户使用手机移动设备访问127.0.0.1/yemian,自动识别到手机端并且跳转至127.0.0.1/m/yemian 2.小二,上代码: //判断是否是移动设备 var ua = na ...

  6. HTML+CSS 整站 步骤

    文件夹管理: CSS JS img font html 根据设计图,划分区块 ,即页面布局 重置样式 ;padding:0;} 写main.css  注意:1 距离尽量使用偶数,避免奇数 2 在使用定 ...

  7. java 函数形参传值和传引用的区别

    java方法中传值和传引用的问题是个基本问题,但是也有很多人一时弄不清. (一)基本数据类型:传值,方法不会改变实参的值. public class TestFun { public static v ...

  8. A题笔记(6)

    No. 3040 代码量好少,主要考到数学知识 唯一需要注意的是变量的类型 int -2147483648 ~ +2147483647 (4 Bytes) long 在32位机器中 int 类型 和 ...

  9. Android简单例子——IpHone样式AlertDialog

    此例子源于网络,下载下来之后,自己加了写注释,作为总结,发到博客中,谢谢原作者 通过这个例子学到的东西 1.自定义对话框的使用 2.程序中使用颜色如何进行存放,增加复用性 3.加深线性布局.常用控件的 ...

  10. fekit前端代码模块化工具

    fekit是一套前端开发工具,是由去哪儿网开发.目前在github上开源.使用fekit的优点: a.本地开发支持环境:从开发调试到上线,均是前后端工程独立开发.调试.部署,打破了原来前后端揉在一个工 ...