Tuesday, June 21, 2011

Email Validation in iPhone

In this post we shall have a look on how to validate emails in iPhone

Design Phase: The design will include a text field and a button, on the button touch we will be associating a function that will validate the email written in the text field, so please make a view which looks like the one given below, you make take help from my earlier tutorial

Step 1: Open Xcode and select the windows based application template give your project a name and then add the UIViewController subclass file into your project with the name MyViewController, and when you are done doing that two new files will be added into your project with the name MyViewController.h and MyViewController.m. Once these two files are added create a view which looks like the one given above and then continue reading step 2.

Step 2: Don’t forget to declare a function in the .h file of the MyViewController where you will be writing code for validating the email which will be present in the text field.

Step 3: Here’s the code to validate the email


NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL b = [emailTest evaluateWithObject:emailtxtfield.text];
    if (b==NO) {
        messageBox = [[UIAlertView alloc]initWithTitle:@"iPhone By Radix" message:@"Invalid Email ID" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [messageBox show];
        [messageBox release];
    else {
        messageBox = [[UIAlertView alloc]initWithTitle:@"iPhone By Radix" message:@"Valid Email ID" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [messageBox show];
        [messageBox release];

Code Explanation: The first string variable emailRegex has the regular expression pattern for validating the email format, regular expressions are special strings for describing a particular pattern.

The next line sets a predicate format so that we can evaluate the text from the text field with the help of the predicates instance function evaluateWithObject: which will return you a Boolean result indicating whether the text entered in the textfield is a valid text or not as per the regular expression pattern given in the emailRegex variable.

messageBox is a instance of the UIAlertView declared in the MyViewController.h file.

Step 4: Select the appdelegate.m file in your project and add this view to the iPhone window here's the code that will help you to do that
#import "EmailValidationDemoAppDelegate.h"

#import "MyViewController.h"

@implementation EmailValidationDemoAppDelegate
@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    // Override point for customization after application launch.
    MyViewController *obj = [[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];
    [self.window addSubview:obj.view];
    [self.window makeKeyAndVisible];
    return YES;

Step 5: Run the application to get the following output.

Invalid Email ID

Valid Email ID

I hope this post has helped you out, Happy iCoding and have a great Day.