1

I am building a simple login system using Tkinter in python, I want to take user email id by validating the user type value should be a email not anything else. How to make that, please any one tell me.

This is the simple version of my code,

from tkinter import *

root = Tk()
root.geometry("400x300")


def signIn():

    if entry_var2.get().isdigit():
        print("emailId should not have only numbers")
    elif entry_var2.get().isalpha():
        print("emailId shold have not only alphabets")
    elif entry_var2.get().isspace():
        print("emailId don't have space")
    else:
        print("Login Success") # But it gives not the output I want


# username
entry_var1 = StringVar()
entry1 = Entry(root, textvariable = entry_var1)
entry1.grid(row=0, column=0)

# email id
entry_var2 = StringVar()
entry2 = Entry(root, textvariable = entry_var2)
entry2.grid(row=0, column=0)

# Sign In
button = Button(root, text="Result", command=signIn)
button.grid(row=1, column=0)

root.mainloop()
Kumara
  • 480
  • 1
  • 4
  • 13

2 Answers2

1

Use regex to check for email strings.

import re

regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
 
 
def signIn():
    if(re.fullmatch(regex, entry_var2.get())):
        print("Valid Email")
    else:
        print("Invalid Email")
  • Yes, it works but not exactly what I want, it says '333333@222.gy' as a valid email but I want to be like a really one (like starts with alphabets and can have combination of numbers, symbols and alphabets). Is it possible with regex or by anything else please tell me. – Kumara Apr 08 '22 at 11:46
  • I think email ids and domain names are allowed to start with numerals. As for symbols only @ and . are allowed in the regex – Aishwarya Patange Apr 09 '22 at 05:45
  • In my case I want it to be mixture of numbers and alphabets, and not values repeating like those. – Kumara Apr 09 '22 at 19:26
0

this is python function to validate email using regex:

import re
 
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'

def check(entry_var2):
    if(re.fullmatch(regex, entry_var2)):
        print("Valid Email")

    else:
        print("Invalid Email")

for full solution look at this article

  • 1
    This is an _exact_ duplicate of [this other answer](https://stackoverflow.com/a/71795267/16775594). Please don't post duplicate answers; see [answer] for how to write a good answer. – Sylvester Kruin Apr 08 '22 at 15:52