About
Home > Tutorials Home > Ios > Form Validation Example in iOS

Form Validation Example in iOS

Published on October 17, 2019 8:31:04 PM

Next Previous Keyboard Example

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.This is the ButtonPressed example. In this example we will see how to load another view after pressing button. So let see how it will worked. Another Buttonpress reference you can find out from here ButtonView in iPhone.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at Eonlinetutors.com make sure that your child gets the perfect learning environment to outperform.




Step 1 : Open the XCode and create a new Single View Based Application template. Give the application name "Button Example". Copy this Code to the ViewController.h

#import <UIKit/UIKit.h>

@interface AbhishekViewController : UIViewController

@property (retain, nonatomic) IBOutlet UITextField *txt1;

@property (retain, nonatomic) IBOutlet UITextField *txt2;

@property (retain, nonatomic) IBOutlet UITextField *txt3;

@property (retain, nonatomic) IBOutlet UITextField *txt4;

@property (retain, nonatomic) IBOutlet UITextField *txt5;

- (IBAction)checkValidation:(id)sender;

@end

Step 2 : Copy the Following Code into your ViewController.m File.

#import "AbhishekViewController.h"

@implementation AbhishekViewController

@synthesize txt1;

@synthesize txt2;

@synthesize txt3;

@synthesize txt4;

@synthesize txt5;

- (void)viewDidLoad
{
    [super viewDidLoad];
	UIToolbar *toolbar = [[UIToolbar alloc]init];
	[toolbar setBarStyle:UIBarStyleBlackTranslucent];
	[toolbar sizeToFit];
	UIBarButtonItem *flexButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
	UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyBoard)];
	UIBarButtonItem *previousbtn = [[UIBarButtonItem alloc]initWithTitle:@"Next >>" style:UIBarButtonItemStyleBordered target:self action:@selector(changeTextBox)];
	UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:@"<< Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backFocus)];
	NSArray *itemsArray = [NSArray arrayWithObjects:previousbtn,backButton, flexButton,doneButton,nil, nil];
	
    [toolbar setItems:itemsArray];
	[txt1 setInputAccessoryView:toolbar];
	[txt2 setInputAccessoryView:toolbar];
	[txt3 setInputAccessoryView:toolbar];
	[txt4 setInputAccessoryView:toolbar];
	[txt5 setInputAccessoryView:toolbar];
}

-(void)backFocus //Previous
{
    if ([txt1 isFirstResponder]) 
	{
        [txt5 becomeFirstResponder];
	}
    else if([txt2 isFirstResponder])
	{
        [txt1 becomeFirstResponder];
	}
    else if([txt3 isFirstResponder])
	{
        [txt2 becomeFirstResponder];
	}
    else if([txt4 isFirstResponder])
	{
        [txt3 becomeFirstResponder];
	}
    else
    {
        [txt4 becomeFirstResponder];
	}
}

-(void) changeTextBox //Next
{
    if ([txt1 isFirstResponder]) 
	{
        [txt2 becomeFirstResponder];
	}
    else if([txt2 isFirstResponder])
	{
        [txt3 becomeFirstResponder];
	}
    else if([txt3 isFirstResponder])
	{
        [txt4 becomeFirstResponder];
	}
    else if([txt4 isFirstResponder])
	{
        [txt5 becomeFirstResponder];
	}
    else
    {
        [txt1 becomeFirstResponder];
	}
}

-(void) resignKeyBoard
{
    [txt1 resignFirstResponder];
	[txt2 resignFirstResponder];
	[txt3 resignFirstResponder];
	[txt4 resignFirstResponder];
	[txt5 resignFirstResponder];
}

- (IBAction)checkValidation:(id)sender
{
    BOOL flag = NO;
    BOOL flag1 = NO;

    if ([[txt1 text]length] == 0)
	{
        flag = YES;
	}
    else
    {
        NSString *myData  = @"[a-zA-Z]*";
		NSPredicate *numbertezt = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",myData];
		if ([numbertezt evaluateWithObject:[txt1 text]] == NO) 
		{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Invalid data" message:@"Only Characters." delegate:nil cancelButtonTitle:@"Okey" otherButtonTitles:nil, nil];
			[alert show];
            flag1 = YES;
            [txt1 setBorderStyle:UITextBorderStyleBezel];
		}
        else
        {
			[txt1 setBorderStyle:UITextBorderStyleRoundedRect];
		}
		if([[txt2 text]length] == 0)
		{
        	flag = YES;
		}
		else
		{
        	NSString *myData  = @"[a-zA-Z0-9]*";
			NSPredicate *numbertezt = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",myData];
			if ([numbertezt evaluateWithObject:[txt2 text]] == NO) 
			{
            	UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Invalid data" message:@"Only Characters and Digits." delegate:nil cancelButtonTitle:@"Okey" otherButtonTitles:nil, nil];
            	[alert show];
            	flag1 = YES;
            	[txt2 setBorderStyle:UITextBorderStyleBezel];
			}
        	else
        	{
            	[txt2 setBorderStyle:UITextBorderStyleRoundedRect];
			}
    	}
    	if([[txt3 text]length] == 0)
		{
        	flag = YES;
		}
		else
		{
        	NSString *myData  = @"[a-zA-Z0-9]*";
			NSPredicate *numbertezt = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",myData];
			if ([numbertezt evaluateWithObject:[txt3 text]] == NO) 
			{
            	UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Invalid data" message:@"Please Enter Correct Data" delegate:nil cancelButtonTitle:@"Okey" otherButtonTitles:nil, nil];
            	[alert show];
            	flag1 = YES;
            	[txt3 setBorderStyle:UITextBorderStyleBezel];
			}
        	else
        	{
            	if([[txt2 text]compare:[txt3 text]])
				{
                	NSLog(@"Hello");
					UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Invalid data" message:@"Password Doesn't Match." delegate:nil cancelButtonTitle:@"Okey" otherButtonTitles:nil, nil];
					[alert show];
					flag1 = YES;
					[txt3 setBorderStyle:UITextBorderStyleBezel];
				}
				else
				{
					[txt3 setBorderStyle:UITextBorderStyleRoundedRect];
				}
			}
    	}
    	if([[txt4 text]length] == 0)
		{
        	flag= YES;
		}
		else
		{
        	NSString *myData  = @"[a-zA-Z]*";
			NSPredicate *numbertezt = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",myData];
			if ([numbertezt evaluateWithObject:[txt4 text]] == NO) 
			{
            	UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Invalid data" message:@"Please Enter Correct Data" delegate:nil cancelButtonTitle:@"Okey" otherButtonTitles:nil, nil];
				[alert show];
				flag1 = YES;
				[txt4 setBorderStyle:UITextBorderStyleBezel];
			}
			else
			{
				[txt4 setBorderStyle:UITextBorderStyleRoundedRect];
			}
		}
    
    	if([[txt5 text]length] == 0)
		{
        	flag = YES;
		}
		else
		{
			NSString *myData  = @"[0-9]{6}";
			NSPredicate *numbertezt = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",myData];
			if ([numbertezt evaluateWithObject:[txt5 text]] == NO) 
			{
            	UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Invalid data" message:@"Only 6 Digit." delegate:nil cancelButtonTitle:@"Okey" otherButtonTitles:nil, nil];
				[alert show];
				flag1 = YES;
				[txt5 setBorderStyle:UITextBorderStyleBezel];
			}
			else
			{
				[txt5 setBorderStyle:UITextBorderStyleRoundedRect];
			}
		}
		if (flag == YES && flag1 == NO) 
		{
        	UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Invalid data" message:@"Please Fill All the Field !!!" delegate:nil cancelButtonTitle:@"Okey" otherButtonTitles:nil, nil];
			[alert show];
			flag = NO;
		}
}

@end
ALL COMMENTS (2)
Abhishek Bedre 2017-03-28 17:25:15
I have implemented my own "jarvis"....thanks to this example.        
2019-01-03 13:22:19
It is working fine in desktop but not working in android mobile