标签:手机 ber turn pre ror integer page rms com
Email:验证数据是否为邮箱
EqualTo:验证此字段的数据是否和另一个字段的值相等,常用与校验密码和确认密码
InputRequired:检验数据必传
Length:校验数据长度
NumberRange:检验数据需在指定数字的区间
Regexp:自定义正则表达式(如手机号码)
URL:必须是url的形式
UUID:必须是UUID
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="#" method="post">
<table>
<tbody>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>手机号:</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td>个人主页:</td>
<td><input type="text" name="home_page"></td>
</tr>
<tr>
<td>uuid:</td>
<td><input type="text" name="uuid"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="text" name="check_password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="点击注册"></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
from wtforms import Form, StringField, IntegerField
from wtforms.validators import Length, EqualTo, Email, InputRequired, NumberRange, Regexp, URL, UUID
class RegistForm(Form):
""" 注册参数验证 """
# form表单中参数的name属性值
username = StringField(validators=[Length(3, 10, message=‘用户名长度为3~10位‘)])
email = StringField(validators=[Email(message=‘非邮箱格式‘)])
password = StringField(validators=[InputRequired(message=‘密码必传‘), Length(6, 10, message=‘密码长度为6~10位‘)])
check_password = StringField(validators=[Length(6, 10, message=‘密码长度为6~10位‘), EqualTo(‘password‘, message=‘两次密码不一致‘)])
age = IntegerField(validators=[NumberRange(18, 50, message=‘年龄需为18~50岁‘)])
phone = StringField(validators=[Regexp(r‘1[34578]\d{9}‘, message=‘手机号格式错误‘)])
home_page = StringField(validators=[URL(message=‘home_page必须为url格式‘)])
uuid = StringField(validators=[UUID(message=‘uuid格式错误‘)])
from flask import Flask, request, render_template
from forms import RegistForm
app = Flask(__name__)
@app.route(‘/regist/‘, methods=[‘GET‘, ‘POST‘])
def regist():
form = RegistForm(request.form)
if request.method == ‘POST‘:
if form.validate():
return ‘验证通过‘
else:
print(form.errors)
return form.errors
return render_template(‘regist.html‘)
if __name__ == ‘__main__‘:
app.run(debug=True)
标签:手机 ber turn pre ror integer page rms com
原文地址:https://www.cnblogs.com/zhongyehai/p/11839110.html