-2

in python how do i fix this

import re

rule = "[A-Z+][\@][A-Z+][\.][A-Z+]"

inputu = input ("please enter your email:").upper()

if re.search (rule, inputu):
    print ("Invalid")

else:
    print ("Valid")

I want to try to make it so you can put A-Z then it looks for @ then A-z then . then A-Z

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    ... And what is a description of the problem you are having? – leekaiinthesky Feb 10 '17 at 22:33
  • 2
    The way to validate an email address is to send it a confirmation email and see if the user does the confirmation thing. Trying to validate the format just leads to false negatives and no real benefit. – user2357112 Feb 10 '17 at 22:38
  • See [Python check for valid email address?](http://stackoverflow.com/questions/8022530/python-check-for-valid-email-address) – Wiktor Stribiżew Feb 10 '17 at 22:46

1 Answers1

2

To make that work what you want to do is move the plus sign outside of the brackets:

rule = r"[A-Z]+\@[A-Z]+\.[A-Z]+"

But note that email addresses can also have other characters, such as numbers and periods. A better rule would be something like:

rule = r"^[A-Z0-9\._+ '\"-]+\@[A-Z0-9]+\.[A-Z0-9]+"

That will check that at the start of the string you have some substring made of letters, numbers, or periods, underscores, dashes, plus signs, spaces or single or double quotes. Then an @. Then another substring made of letters and numbers. Then a period. Then finally another substring made entirely of letters at the end of the string.

But note that this still doesn't mean that the address is valid. Only that it might be. If you really do need to validate an email address the only way is to send them an email, and get them to respond.

Batman
  • 8,571
  • 7
  • 41
  • 80
  • 1
    It might be worthwhile to reference [the standard](https://tools.ietf.org/html/rfc5322#section-3.4). Otherwise, you might miss `"djt jr"@wh.gov`. Note the quotes and embedded space. Also, why the length check on the TLD? What about `ansel@adams.photography`? – Robᵩ Feb 10 '17 at 22:40
  • Cheers mate. Wasn't pretending to know all the options, just suggesting that would be a better rule. I'll update the answer though. – Batman Feb 10 '17 at 22:45
  • Thankyou! however i just ment the format and theoretically so it doesnt matter if its not real but thankyou – Jack Fitton Feb 11 '17 at 17:03