Login Form with Password Validation

  • Share this:

Code introduction


This code defines a login form class based on the wtforms library, including username and password fields, and performs a password length validation.


Technology Stack : Code using package and technology stack[English]

Code Type : The type of code

Code Difficulty : Advanced


                
                    
from wtforms import Form, StringField, PasswordField, validators

def validate_password_length(form, field):
    if len(field.data) < 6:
        raise validators.ValidationError('Password must be at least 6 characters long.')

def create_login_form():
    class LoginForm(Form):
        username = StringField('Username', [validators.Length(min=4, max=25)])
        password = PasswordField('Password', [
            validators.DataRequired(),
            validate_password_length
        ])

    return LoginForm