Here is my code:
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')
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.
WTForms is a Python library that provides flexible web form rendering. You can use it to render text fields, text areas, password fields, radio buttons, and others. WTForms also provides powerful data validation using different validators, which validate that the data the user submits meets certain criteria you define.
Regular expressions describe patterns that match combinations of characters in strings. They have a variety of applications, such as validating user input from HTML forms. You can use regex to validate with JavaScript or via the HTML pattern attribute.
RegEx validation is essentially a syntax check which makes it possible to see whether an email address is spelled correctly, has no spaces, commas, and all the @s, dots and domain extensions are in the right place.
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/
Replacing the regex with
'^\w+$'
solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With