Search code examples
pythonuser-interfacetkinterpasswords

Tkinter Password Verification


Hi I am fairly new to tkinter but I am making a log in/sign up gui and on the sign up window I wanted the password entry to be verified, in a way that cannot contain spaces and needs to be at least 8 characters long.

def sign_up_clicked_r():
    global inv_user_r
    global inv_pass_r
    global alr_user_r
    user_r = user_entry_r.get()
    password_r = password_entry_r.get()

    if not user_r and not password_r:
        inv_user_r = canvas.create_text(
            252.0, 259.0, text="Username cannot be empty!", fill="#9f0000",
            font=("Poppins Regular", int(11.0 * -1.1)), anchor="nw")
        inv_pass_r = canvas.create_text(
            252.0, 340.0, text="Password cannot be empty!", fill="#9f0000",
            font=("Poppins Regular", int(11.0 * -1.1)), anchor="nw")
        canvas.after(1750, clear_inv_r)
        return
    if user_r and not password_r:
        inv_pass_r = canvas.create_text(
            252.0, 340.0, text="Password cannot be empty!", fill="#9f0000",
            font=("Poppins Regular", int(11.0 * -1.1)), anchor="nw")
        canvas.after(1750, clear_invp_r)
        return
    if not user_r and password_r:
        inv_user_r = canvas.create_text(
            252.0, 259.0, text="Username cannot be empty!", fill="#9f0000",
            font=("Poppins Regular", int(11.0 * -1.1)), anchor="nw")
        canvas.after(1750, clear_invu_r)
        return

This is a part of the code when after inserting the details on the text boxes I click on the sign up button and makes all this verification process, and as I said I wanted to add those 2 new features to it. Can anyone help me with this?


Solution

  • Check for empty spaces:

    if password_r.find(" ") != -1:
        inv_user_r = canvas.create_text(
            252.0, 259.0, text="Pasword cannot contain any spaces!", fill="#9f0000",
            font=("Poppins Regular", int(11.0 * -1.1)), anchor="nw")
        canvas.after(1750, clear_invu_r)
        return
    

    Check for length:

    if len(password_r) < 8:
        inv_user_r = canvas.create_text(
            252.0, 259.0, text="Password needs to be at least 8 characters!", fill="#9f0000",
            font=("Poppins Regular", int(11.0 * -1.1)), anchor="nw")
        canvas.after(1750, clear_invu_r)
        return
    

    Check for both:

    if password_r.find(" ") != -1 or len(password_r) < 8:
        inv_user_r = canvas.create_text(
            252.0, 259.0, text="Password needs to be at least 8 characters and cannot contain spaces!", fill="#9f0000",
            font=("Poppins Regular", int(11.0 * -1.1)), anchor="nw")
        canvas.after(1750, clear_invu_r)
        return