Verify email address in UITextField on iphone

Possible duplicate:
Make sure the user has entered the email address string in the correct format?

I have a UITextField in which I take the email address from the user they enter, and I want to check this email address, for example, I would like to check that it contains characters such as the @ sign and other email characters.

If there is an error in the email address, then it should show a UIAlertView that says "enter a valid email address".

+8
objective-c uitextfield
source share
3 answers

Objective-C Style

 NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,10}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx]; if ([emailTest evaluateWithObject:email.text] == NO) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test!" message:@"Please Enter Valid Email Address." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; return; } 

Swift style

 class func isValidEmail(emailString:String) -> Bool { let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,10}" var emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) let result = emailTest?.evaluateWithObject(emailString) return result! } 
+22
source share

You can do this using NSPredicate

 //suppose emailID is your entered email address NSString NSString *emailFormat1 = @"[A-Z0-9a-z._]+@[A-Za-z0-9]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailFormat1]; if ([emailTest1 evaluateWithObject:emailID]||[emailTest2 evaluateWithObject:emailID]) { //yes it is valid } else //no it is invalid 
+3
source share

Add RegexKitLite to your project and find the solutions below.

  • Objective-C email address verification guidelines on iOS 2.0
  • How to check email field in uitextfield in iphone
+1
source share

All Articles