4

Assuming I have created IBOutlet UITextField *emailValidate;

And the empty method

-(IBAction)checkEmail:(id)sender {

// Add email validation code here.

}

And linked the File Owner file to the TextField, what code would I have to insert in the method to validate an email adress? checking that only one '@' is included, and only one '.' is included?

Macarse
  • 91,829
  • 44
  • 175
  • 230
Anthony
  • 123
  • 2
  • 9
  • 3
    Which of the three '.' characters in `graham.lee@example.co.uk` is the one that is allowed? –  Feb 17 '11 at 15:45
  • only one `.`? are you implying the millions of firstName.lastName@provider.tld are not valid? – Matthias Bauch Feb 17 '11 at 15:45
  • possible duplicate of [Best practices for validating email address in Objective-C?](http://stackoverflow.com/questions/800123/best-practices-for-validating-email-address-in-objective-c) –  Feb 17 '11 at 16:49

6 Answers6

17

Use the function below...

+(BOOL) validateEmail: (NSString *) email 
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL isValid = [emailTest evaluateWithObject:email];
return isValid;
}
Suresh Varma
  • 9,750
  • 1
  • 60
  • 91
2

In my case I use a regex found at this blogpost:

NSString *emailRegEx =
    @"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
    @"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
    @"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
    @"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
    @"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
    @"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
    @"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
Macarse
  • 91,829
  • 44
  • 175
  • 230
0

You can get set of code from the following link . Hope this may helpful

Sugan S
  • 1,782
  • 7
  • 25
  • 47
0

I've used the solution shared by Macarse (the big regexp) for a few weeks with success, but I suddenly ran into a problematic case. It does not pass the test with "test1_iPhone@neywen.net" for instance. So I chose to go back to the simpler solution provided by S P Varma (the small and simple regexp).

neywen
  • 121
  • 4
0

You can determine if there is exactly one "@" by splitting the string on '@' and checking for 2 pieces.

int numberOfAtPieces = [[emailValidate.text  componentsSeparatedByString:@"@"] count];
if ( numberOfAtPicess != 2 ) {  // show error alert }
else {  // let it through }
Rayfleck
  • 12,116
  • 8
  • 48
  • 74
-1

You could call the following method on the text of the UITextField:

- (BOOL)validateEmail:(NSString *)candidate {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 

    return [emailTest evaluateWithObject:candidate];
}

Please adapt the emailRegex regular expression to your needs.

Sylvain
  • 1,553
  • 11
  • 15
  • 3
    I hope you upvoted catlan for this ;-) http://stackoverflow.com/questions/800123/best-practices-for-validating-email-address-in-objective-c/1149894#1149894 – Matthias Bauch Feb 17 '11 at 15:48
  • 1
    That regex will not work: it will strip out a lot of valid e-mails. – ipartola Mar 03 '11 at 19:16