Email Validation with domain

S

Sallu

Hi All, import re
msg=raw_input('Enter the email : ')

def validateEmail(email):

#if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]
{1,3})(\\]?)$", email) != None:
if re.match("^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$",
email) != None:
print 'Valis'
else:
print 'not'

validateEmail(msg) i wrote a script above it works fine but it does
not check for valid domain like .com .org .in
how to validate with domain
 
O

oj

Hi All,   import re
msg=raw_input('Enter the email : ')

def validateEmail(email):

        #if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]
{1,3})(\\]?)$", email) != None:
        if re.match("^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$",
email) != None:
                print 'Valis'
        else:
                print 'not'

validateEmail(msg)  i wrote a script above it works fine but it does
not check for valid domain like .com .org .in
how to validate with domain

Don't try and check that the TLD (.com .org etc.) is valid with a
regular expression.

Just don't.

Especially now that the rules about domain names are now being relaxed
and there is no possible way of you predicating what all the new TLDs
will be.

Validating that that the e-mail address is in a valid form, and
validating the domain is valid are two separate things.

If you want to validate the domain, do a DNS lookup on the domain or
some such. I don't think there are standard modules to provide this
functionality included with python. You could try using the socket
module, and reading up on the relevant protocols, or making calls to
external programs.
 
L

livibetter

If you want to validate the domain, do a DNS lookup on the domain or
some such. I don't think there are standard modules to provide this
functionality included with python. You could try using the socket
module, and reading up on the relevant protocols, or making calls to
external programs.

I agreed. I made quick code for this.

# Email address validator
#
# This module is in Public Domain
#
# This module was written for replying to
# http://groups.google.com/group/comp.lang.python/browse_thread/thread/80d7d31bebc09190
# * It requires dnspython (http://www.dnspython.org/).
# * It is a simple prototype.
# * It would be slow if query mass email addresses, having cache
mechanism
# would be very helpful.
# * It only checks hostname of email address.
#
# Author : Yu-Jie Lin
# Creation Date: 2008-07-02T20:09:07+0800


import dns.resolver


def CheckEmail(email):
"""This function directly extracts the hostname and query it"""
email_parts = email.split('@')
if len(email_parts) != 2:
return False

# Start querying
try:
answers = dns.resolver.query(email_parts[1], 'MX')
except dns.resolver.NoAnswer:
# This host doesn't have MX records
return False
except dns.resolver.NXDOMAIN:
# No such hostname
return False

# Possible a valid hostname
return True

I also wrote a short blog post for this post and the code, if you are
interested, you can read it at http://thetinybit.com/Blog/2008-07-02-2047-EmailHostnameCheck
 
S

Sallu

Actually, no. It rejects a great many email addresses that are valid.


To validate a domain for delivery of email, check with the DNS by
requesting the A or MX record for that domain.

To validate an email address, check with the mail server for that
domain by sending a message to the address.

Neither of them should be "validated" by a regular expression.

Please refer to RFC 3696 <URL:http://www.ietf.org/rfc/rfc3696.txt>
described as "Recommended techniques for applications checking or
manipulating domain and other internet names".

--
 \        “Pinky, are you pondering what I'm pondering?” “Wuh, I think |
  `\   so, Brain, but wouldn't anything lose its flavor on the bedpost |
_o__)                               overnight?” —_Pinky and The Brain_ |
Ben Finney

Thank you to all of you and clearing my idea..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,081
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top