By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,673 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,673 IT Pros & Developers. It's quick & easy.

Email Validation with domain

P: n/a
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
Jul 2 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a
oj
On Jul 2, 12:41*pm, Sallu <praveen.sunsetpo...@gmail.comwrote:
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.
Jul 2 '08 #2

P: n/a
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....d7d31bebc09190
# * 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-0...lHostnameCheck
Jul 2 '08 #3

P: n/a
Sallu <pr*****************@gmail.comwrites:
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
Jul 2 '08 #4

P: n/a
On Jul 2, 6:25*pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
Sallu <praveen.sunsetpo...@gmail.comwrites:
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
Thank you to all of you and clearing my idea..
Jul 3 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.