An email address identifies an email box to which gives any boolean value and then after u can perform any operation further. An example format of an email address islewis@example.net which is read into textfield and follwing example checks email validation.

below source code for the validating the email id useing the Regular Expression.

In this method putting the Regular Expresion

1
2
3
4
5
6
- (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 0;
    return [emailTest evaluateWithObject:candidate];
}

In this method to call regular expresion method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-(IBAction)btnTapped:(id)sender{
    if([self validateEmail:[txtEmail text]] ==1)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"You Enter Correct Email id." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
    }
    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"You Enter Incoorect Email id." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
    }
}

I have placed a screen-shots for the sample output of this tutorial.

Grab the Code

Thanks & Best Regards,
Nimit Parekh.

Advertisements