from wtforms.validators import DataRequired, Length, Email, Regexp, EqualTo,InputRequired,IPAddress
验证数据是否为空:
DataRequired()
验证字段是否输入了内容:
InputRequired()
验证IP是否为有效IP ipv4默认为True ipv6 默认False
IPAddress()
验证字符串长度:
Length(min =1,max = 5)
验证是否为有效的Mac地址:
MacAddress()
验证数字是最小值和/最大值,适用于任何类似的数字类型,例如浮点数和小数,而不仅仅是整数。
NumberRange(min = None,max = None,message = None )
正则表达式验证:
Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,'Usernames must have only letters, ''numbers, dots or underscores')
比较两个值验证:
password = PasswordField('Password', validators=[DataRequired(),
EqualTo('password2', message='Passwords must match.')])
password2 = PasswordField('Confirm password', validators=[DataRequired()])
验证Email是否有效
Email()
自定义验证器:
def validate_email(self, field):
if User.query.filter_by(email=field.data).first():
raise ValidationError('Email already registered.')