使用 WTForms 和 python 进行正则表达式验证 [英] Regex validation with WTForms and python
本文介绍了使用 WTForms 和 python 进行正则表达式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
class CreateUser(Form):
username = StringField('Username', [
validators.Regexp('\w+', message="Username must contain only letters numbers or underscore"),
validators.Length(min=5, max=25, message="Username must be betwen 5 & 25 characters")
])
password = PasswordField('New Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Passwords must match')
])
confirm = PasswordField('Repeat Password')
所以问题存在于第 3 行.我希望用户名只能是字母数字字符.出于某种原因,这个正则表达式只检查第一个字符.+ 符号在这里不起作用有什么原因吗?谢谢.
So the problem exists at line 3. I want the username to be only alpha numeric characters. For some reason this regex is only checking the first character. Is there a reason why the + symbol is not working here? Thanks.
推荐答案
用
'^\w+$'
解决了问题.
这篇关于使用 WTForms 和 python 进行正则表达式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文