0

I would like to validate the data that the user has entered in my program (an email address). If the user enters an '@' symbol, the program should accept it. If it does not, the program should loop and ask for the email again. So far I have attempted to do this, but the program loops for every character that has been entered:

emailright = True

while emailright == True:    
    email = input("Please enter your email address")
    character = ("@")
    for character in email:
        if character == '@':
            print("Your email address had been registered")
            emailright = True
        else:
            print("invalid email address, please re-enter")
            emailright = False

The while loop and everything inside it has also been indented. Thank you!

3 Answers3

3

While there are many better email validation methods, this implements your logic much more concisely:

email = input("Please enter your email address")
while '@' not in email:    
    email = input("invalid email address, please re-enter")
print("Your email address had been registered")
user2390182
  • 72,016
  • 6
  • 67
  • 89
0

something like this?

import re


is_email_valid = None


while is_email_valid is None:
    email = input("Please enter your email address")
    is_email_valid = re.match('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', email)
    if is_email_valid:
        print("Your email address had been registered")
    else:
        print("invalid email address, please re-enter")

this is a site where I extracted the regex from:

http://emailregex.com/

Pippo
  • 905
  • 11
  • 22
-3
while True:    
    email = input("Please enter your email address")
    if '@' in email:
        break

Or:

https://pypi.python.org/pypi/validate_email

Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45