I have made an iphone application in which I have used email id validation. The code which I have used is as below:
NSString *valid = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", valid];
if ([emailTest evaluateWithObject:Id.text] == NO)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Validation" message:@"Email Id is not Valid" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
if ([emailTest evaluateWithObject:Id.text] == YES)
{
NSString *b = Id.text;
NSLog(@"value of the Id is : %@",b);
if([b length] != 0)
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
//[picker setSubject:@"Your iPhone fan"];//@"Hello iPhone!"
[picker setToRecipients:[NSArray arrayWithObject:b]];
NSString *emailBody =@""; //@"";//@"Nice to See you!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
But if I put 1@gmail.com in textfield then it still shows valid id but in real it is invalid email id.