Creating a Login Form with Password Validation

  • Share this:

Code introduction


This function creates a login form with username and password fields. The password field must match the confirm password field and has a length between 6 to 25 characters.


Technology Stack : wtforms

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
from wtforms import Form, StringField, PasswordField
from wtforms.validators import DataRequired, Length, EqualTo

def create_login_form():
    class LoginForm(Form):
        username = StringField('Username', validators=[DataRequired()])
        password = PasswordField('Password', validators=[
            DataRequired(),
            Length(min=6, max=25),
            EqualTo('confirm_password', message='Passwords must match')
        ])
        confirm_password = PasswordField('Confirm Password')

    return LoginForm                
              
Tags: