Registration Form with Username and Password Validation

  • Share this:

Code introduction


This code defines a registration form with username and password fields, along with corresponding validators. The username has a length restriction of 4-25 characters, and the password has a length restriction of 6-35 characters, which must match the confirm password.


Technology Stack : wtforms

Code Type : Form Creation

Code Difficulty : Intermediate


                
                    
from wtforms import Form, StringField, PasswordField, validators

def create_form():
    class RegistrationForm(Form):
        username = StringField('Username', [validators.Length(min=4, max=25)])
        password = PasswordField('Password', [
            validators.DataRequired(),
            validators.EqualTo('confirm', message='Passwords must match'),
            validators.Length(min=6, max=35)
        ])
        confirm = PasswordField('Repeat Password')

    return RegistrationForm                
              
Tags: