Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Email Validation with domain

16 views
Skip to first unread message

Sallu

unread,
Jul 2, 2008, 7:41:08 AM7/2/08
to
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

oj

unread,
Jul 2, 2008, 8:33:14 AM7/2/08
to

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.

livibetter

unread,
Jul 2, 2008, 8:52:07 AM7/2/08
to

> 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

Ben Finney

unread,
Jul 2, 2008, 9:25:24 AM7/2/08
to
Sallu <praveen.s...@gmail.com> writes:

> validateEmail(msg) i wrote a script above it works fine

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

> but it does not check for valid domain like .com .org .in how to
> validate with domain

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

Sallu

unread,
Jul 3, 2008, 1:38:11 AM7/3/08
to
On Jul 2, 6:25 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:

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

0 new messages