views:

82

answers:

5

I am developing an iPhone application where I need the user to give his email address at login.

What is the best way to check if an email address is a valid email address?

+1  A: 

to validate the email string you will need to write a regular expression to check it is in the correct form. there are plenty out on the web but be carefull as some can exclude what are actually legal addresses.

essentially it will look something like this

^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$

Actually checking if the email exists and doesn't bounce would mean sending an email and seeing what the result was. i.e. it bounced or it didn't. However it might not bounce for several hours or not at all and still not be a "real" email address. There are a number of services out there which purport to do this for you and would probably be paid for by you and quite frankly why bother to see if it is real?

It is good to check the user has not misspelt their email else they could enter it incorrectly, not realise it and then get hacked of with you for not replying. However if someone wants to add a bum email address there would be nothing to stop them creating it on hotmail or yahoo (or many other places) to gain the same end.

So do the regular expression and validate the structure but forget about validating against a service.

PurplePilot
That's one hell of a regular expression. To use it, you'll either need to target iOS 4.0 which has the NSRegularExpression class, or use one of the many regex static libraries compiled for previous versions of iOS.
Alex
Actually, you can use NSPredicate, which can handle regular expressions.
BadPirate
A: 

Have the type in input-tag as email instead of text.

<input type="email" />
Codler
-1 The "objective-c" tag should've made this fairly obvious that @raaz wasn't talking about HTML...
Dave DeLong
A: 

You should take a a look at our email validation web-service. The main features are that it:

•checks in real-time that the email domain exists and can accept messages

•suggests corrections for misspelt domain names

•suggests corrections for misspelt user names

•identifies email addresses as personal, generic, free etc

http://www.data-8.co.uk/integr8/services/email_validation.aspx

Cheers,

Jez

Jez Walker
Sorry? You suggest corrections for misspelt user and domain names? How on earth can you do that reliably??? Sounds like a sure-fire data protection issue to me!
DaveyDaveDave
I wonder too, how misspelt user names should be detected. I guess this can flag only invalid characters. Suggestions for misspelt domains could come from some kind of dictionary, this shouldn’t be too much trouble.
Sven
A: 

Finally i got my answer at this stack overflow thread

raaz
+1  A: 

Good cocoa function:

BOOL NSStringIsValidEmail(NSString *checkString)
{
    BOOL sticterFilter = YES; // Discussion http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/

    NString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\.[A-Za-z]{2}[A-Za-z]*";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:checkString];
}

Discussion on Lax vs. Strict - http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/

BadPirate