Regex Validation With Wtforms And Python
Solution 1:
Replacing the regex with
'^\w+$'
solved the problem.
Solution 2:
I know this was answered a long time ago, but another option I discovered to provide alphanumeric validation on WTForms is AlphaNumeric()
from wtforms_validators import AlphaNumeric
...
class SignupForm(Form):
login_id = StringField('login Id', [DataRequired(), AlphaNumeric()])
More details here https://pypi.org/project/wtforms-validators/
Solution 3:
@mpn solution works fine but I was wondering why \w+
is not matching the whole string.
In wtforms' Regexp class whitin the method __call__
you will see that self.regex.match(field.data or '')
will be called. The documentation of re.py states about re.match
that:
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object.
Hence, we will have to use ^
to match the start of the string and $
to match the end of the string. Read more about the special characters in the documentation.
Post a Comment for "Regex Validation With Wtforms And Python"