Advertisement
Nenogzar

email_validator

Jun 9th, 2024
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. import tkinter as tk
  2.  
  3.  
  4. def validate_email():
  5.     # Hide all error labels
  6.     valid_label.place_forget()
  7.     username_error.place_forget()
  8.     symbol_at_error.place_forget()
  9.     domain_error.place_forget()
  10.     more_at_error.place_forget()
  11.  
  12.     email = input_email.get()
  13.  
  14.     if len(email.split('@')[0]) < MIN_SYMBOLS_USERNAME:
  15.         username_error.place(x=150, y=50)
  16.         return
  17.     elif '@' not in email:
  18.         symbol_at_error.place(x=200, y=50)
  19.         return
  20.     elif email.count('@') > 1:
  21.         more_at_error.place(x=180, y=50)
  22.         return
  23.     elif not any(email.endswith(f".{domain}") for domain in VALID_DOMAINS):
  24.         domain_error.place(x=100, y=50)
  25.         return
  26.  
  27.     valid_label.place(x=210, y=50)
  28.  
  29.  
  30. MIN_SYMBOLS_USERNAME = 5
  31. VALID_DOMAINS = ('com', 'bg', 'net', 'org')
  32.  
  33.  
  34. window = tk.Tk()
  35. window.title('Email Validator')
  36. window.geometry('500x100')
  37.  
  38. entry_label = tk.Label(text='Enter your email:', fg='#463EAC')
  39. entry_label.pack()
  40.  
  41. input_email = tk.Entry(window, width=15)
  42. input_email.place(x=200, y=20)
  43.  
  44. generate_button = tk.Button(window, text='Validate', height=1, command=validate_email, fg='white', bg='blue')
  45. generate_button.place(x=300, y=18)
  46.  
  47. username_error = tk.Label(window, text='Username must be more than 4 characters.\nTry again!', fg='Red')
  48. symbol_at_error = tk.Label(window, text='Email must contain @!', fg='Red')
  49. more_at_error = tk.Label(window, text='Email must contain only one @!', fg='Red')
  50. domain_error = tk.Label(window, text=f'Domain must be one of the following: '
  51.                                      f'{", ".join("." + domain for domain in VALID_DOMAINS)}', fg='Red')
  52.  
  53. valid_label = tk.Label(window, text='Email is valid!', fg='Blue')
  54.  
  55. window.mainloop()
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement