You can download this code by clicking the button below.
This code is now available for download.
This code defines a custom form validation function `validate_username` that checks if the username is alphanumeric and raises a validation error if it is not. It also defines a form class `RegistrationForm` that includes a username field using the `validate_username` function for validation.
Technology Stack : wtforms
Code Type : Custom form validation function
Code Difficulty : Intermediate
from wtforms import Form, StringField, validators
def validate_username(form, field):
if not field.data.isalnum():
raise validators.ValidationError('Username must be alphanumeric.')
class RegistrationForm(Form):
username = StringField('Username', [validate_username])
# JSON representation