views:

834

answers:

4

I like to check the validation of an email address with ajax by onblur? (asp.net,c# and...json or anyother I don't know)

I thought it could be possible with json and calling a webservice. but are there any (free)services to call? or I have to write my own? Hope you understand me what I want.

Thanks

+3  A: 

If you just want to validate the format of an email address, you can do it using regular expressions in javascript without having to do any ajax calls. Check out this related thread

If instead, you want to check whether an email address is real, I'm afraid it isn't possible. There isn't a database of all existing email addresses.

Chetan Sastry
A: 

There's no real way to confirm an email really exist other than sending it an email and seeing if you get a bounce-back (And even then some domains have catch-all's configured so it may not actually be an email).

You can validate the format of it to make sure it looks vaguely like an email. To truly validate an email takes an ungodly complex regex (I've yet to see one that truly encompasses the scope), but a simple check that'll rule out most keyboard-mashing is pretty easy. I typically use:

^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$

There's a lot of fake emails it will accept, but it does a reasonable job of forcing the user to enter something legitimate. Be wary if using a more complex regex that you don't accidentally ban any legitimate addresses, the email spec is very broad in what's allowed.

You could also try doing a DNS lookup on the host portion of the email (After the @) for an extra level of protection, but I'm not sure how much you'll get from this as you'll just force them to lie with a real domain name.

Tim Schneider
he specifically said he's not looking for regex validation of syntax.
theraccoonbear
+1. This is still a reasonable answer for a not-so-clear question.
Chetan Sastry
suggesting something the OP specifically indicated they DIDN'T want is a "reasonable answer"?
theraccoonbear
He just says he wants to validate it server-side. I thought it was a bit vague on what he wanted to validate, plus the most implied option really isn't feasible, so I gave more than one option.
Tim Schneider
it's really ok thanks.. i know i have to implement. a liitle regex...but something like the dns lookup.. thats what I searched or anithing near this... but how to do a dns lookup by ajax? with asp.net?.. thanks all
snarebold
To get decent performance for an OnBlur event you'd likely have to avoid the UpdatePanel style model of AJAX and instead actually interact with JSON. Personally for something like that I'd use http://docs.jquery.com/GetJSON to do the AJAX postback and use either a webmethod or an ASHX to hook into on the .NET side. For the actual DNS lookup you could simply use http://msdn.microsoft.com/en-us/library/system.net.dns.gethostentry.aspx
Tim Schneider
There's no limit to a TLD's length (though I think 2 is the minimum) so you should remove that 6. Also, there does not need to be a subdomain. `n@ai` is a real email address for the guy who maintains the ai TLD.
Eli Grey
Fair call (6 is the longest that exists, .museum I think it is, though it may not be in future), but once you allow all that you're really not validating much beyond that there's an @ in there. I guess it's somewhat a judgment call. I know the clients I work for would call it a bug if it accepted a@bc, but that theoretically could be a real email. I suspect most people with an email on a TLD would be used to using a second address for signing up on web forms (Not that they should have to, but it is a seriously small edge-case).
Tim Schneider
+3  A: 

The whole notion of whether an e-mail address "exists" is somewhat ambiguous. Does an auto-responder address "exist"? Does an address which can receive mail that is immediately discarded "exist"? Some mail servers can be effectively probed by initiating a mail delivery session and seeing if the mail server rejects the address as illegitimate. More savvy mail servers may quietly accept and discarded mail destined for "bogus" addresses to prevent address validation (i.e. to keep spammers from getting "good" addresses). What you're asking is non-trivial, and there's not really a clear cut answer.

theraccoonbear
+2  A: 

Checking if the email address is legitimate is hard. Although there are some techniques you can use. Here is a simple technique that uses an image to make sure that the email address is a correct one. Please note that there are many ways this technique will fail. This includes the email server not allowing the images to be downloaded. But any how you can check out the following technique.

http://azamsharp.com/Posts/153%5FGet%5FNotified%5FWhen%5FUser%5FChecks%5FEmail.aspx

azamsharp