0

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

Marc
  • 2,659
  • 3
  • 34
  • 41
  • Do you mean the format of the email or if the email address exists on the mailserver? – Ralf de Kleine Nov 24 '09 at 22:24
  • 1
    This question is unclear. What exactly are you trying to accomplish? I suspect it is something impossible, as Chetan mentions. Please narrow down the focus of your question... is it about email validation? Or is it about AJAX? Or is it about Active Directory or some similar system? – Bryan Nov 24 '09 at 22:31
  • Im sorry. I wanna check if the given email address really exists.. in example same like sending a test mail so its true if you get no delivery error. but this is of course not possible because by testing I won't send mail :).... hope you understand... so one of latests answers tells about a DNS check or anything.. Think something in this way thats it.. but don't know how and don't like to pay for a service. – Marc Nov 26 '09 at 19:26

3 Answers3

3

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.

Community
  • 1
  • 1
Chetan S
  • 23,637
  • 2
  • 63
  • 78
3

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
  • 4,283
  • 3
  • 33
  • 41
2

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.

fyjham
  • 7,004
  • 2
  • 32
  • 40
  • he specifically said he's not looking for regex validation of syntax. – theraccoonbear Nov 24 '09 at 22:33
  • +1. This is still a reasonable answer for a not-so-clear question. – Chetan S Nov 24 '09 at 22:36
  • suggesting something the OP specifically indicated they DIDN'T want is a "reasonable answer"? – theraccoonbear Nov 24 '09 at 22:41
  • 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. – fyjham Nov 24 '09 at 22:44
  • 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 – Marc Nov 24 '09 at 23:23
  • 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 – fyjham Nov 25 '09 at 05:42
  • 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 Nov 26 '09 at 21:53
  • 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). – fyjham Nov 27 '09 at 00:10