Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-12-2010, 01:05 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 9
zacharyrs is on a distinguished road
Default Help With Multiple Text Field Valdiation

I have five fields I need to validate, as different fields require a different range of numbers to have input by the user. The idea is when the user clicks to the next field it checks the previous field, however this isn't how it's working Is there a better way in my code to do this? I'd also like it when the user hits wrong info and disregards the error messages, it doesn't allow them to go to the next field.

Below is my code:

Code:
- (void)textFieldDidEndEditing:(UITextField *)textField
{	
	//NSLog(@"BundlePath: %@", [[NSBundle mainBundle] bundlePath]);
	
	//declare the vars
	NSString *userNameOne = txtUserName.text;
	float numOne = [userNameOne intValue];			//age in years
	NSString *userNameTwo = txtUserName2.text;
	float numTwo = [userNameTwo intValue];			//iop
	NSString *userNameThree = txtUserName3.text;
	float numThree = [userNameThree intValue];		//cct
	NSString *userNameFour = txtUserName4.text;	
	float numFour = [userNameFour intValue];		//psd
	NSString *userNameFive = txtUserName5.text;
	float numFive = [userNameFive intValue];

	
	if(numOne < 40 || numOne > 100)
	{
			
		//play sound and vibrate for alert
		NSString *bonkSoundFile = [[NSBundle mainBundle] pathForResource:@"alertSound" ofType:@"mp3"];
		NSURL *fileURL = [NSURL fileURLWithPath:bonkSoundFile];
		SystemSoundID  bonkSoundID;
		AudioServicesCreateSystemSoundID( (CFURLRef) fileURL, &bonkSoundID);
		AudioServicesPlaySystemSound(bonkSoundID);
		AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);	//vibrate
		
		//show alert
		UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"Age Error"
							  message:@"Your age must be at least 40 years old and less than 100 years old"
							  delegate:nil
							  cancelButtonTitle:@"OK"
							  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	
	else if (numTwo < 20 || numTwo > 32)
	{
		UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"IOP Error"
							  message:@"Your IOP must be between 20 and 32"
							  delegate:nil
							  cancelButtonTitle:@"OK"
							  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	
	else if (numThree < 475 || numThree > 650)
	{
		UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"CCT Error"
							  message:@"Your CCT must be between 475 and 650"
							  delegate:nil
							  cancelButtonTitle:@"OK"
							  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	
	else if (numFour < .50 || numFour > 3.00)
	{
		UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"PSD Error"
							  message:@"Your PSD must be between .50 and 3.00"
							  delegate:nil
							  cancelButtonTitle:@"OK"
							  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	
	else if (numFive < 0.80)
	{
		UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"C/D Error"
							  message:@"Your C/D must be between 0 and .80"
							  delegate:nil
							  cancelButtonTitle:@"OK"
							  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}
And you can see a video of how it works (rather buggy)
2010-01-12_1258

Any advice would be greatly appreciated!
zacharyrs is offline   Reply With Quote
Old 01-12-2010, 01:24 PM   #2 (permalink)
Will Work for Food!
 
itzdark's Avatar
 
Join Date: Apr 2009
Posts: 579
itzdark is on a distinguished road
Send a message via AIM to itzdark Send a message via MSN to itzdark
Default

instead of one big if else if statement split them up and only validate them if the associated textfield ended editing if(textField==txtUserName)
__________________

Check out my apps

Developers, check out study buddy. I use it everytime I code. It's great for those late night coding sessions.
Unofficial Ad Hoc Distribution Guide || Join my cooperative ad hoc testing group
iSoothe Promotional Video
Contact Me
itzdark is offline   Reply With Quote
Old 01-12-2010, 05:08 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 9
zacharyrs is on a distinguished road
Default

Quote:
Originally Posted by itzdark View Post
instead of one big if else if statement split them up and only validate them if the associated textfield ended editing if(textField==txtUserName)
Yes! Great point! I have updated the code as seen below. Any way to modify this so that if the user doesn't input correct amount, it won't go to next field or allow the option to hit next?

Code:
- (void)textFieldDidEndEditing:(UITextField *)textField
{	
	
	if (textField == txtUserName)
	{
		NSString *userNameOne = txtUserName.text;
		double numOne = [userNameOne intValue];	

			if(numOne < 40 || numOne > 100)
			{
			
				//play sound and vibrate for alert
				NSString *bonkSoundFile = [[NSBundle mainBundle] pathForResource:@"alertSound" ofType:@"mp3"];
				NSURL *fileURL = [NSURL fileURLWithPath:bonkSoundFile];
				SystemSoundID  bonkSoundID;
				AudioServicesCreateSystemSoundID( (CFURLRef) fileURL, &bonkSoundID);
				AudioServicesPlaySystemSound(bonkSoundID);
				AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);	//vibrate
		
				//show alert
				UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"Age Error"
							  message:@"Your age must be at least 40 years old and less than 100 years old"
							  delegate:nil
							  cancelButtonTitle:@"OK"
							  otherButtonTitles:nil];
				[alert show];
				[alert release];
			}
		}
	
	else if (textField == txtUserName2)
	{
		NSString *userNameThree = txtUserName2.text;
		float numTwo = [userNameThree intValue];	
			
		if (numTwo < 20 || numTwo > 32)
		{
			UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"IOP Error"
							  message:@"Your IOP must be between 20 and 32"
							  delegate:nil
							  cancelButtonTitle:@"OK"
							  otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
	}
	
	else if (textField == txtUserName3)
	{
		NSString *userNameThree = txtUserName3.text;
		float numThree = [userNameThree intValue];
		
		if (numThree < 475 || numThree > 650)
		{
		
			UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"CCT Error"
							  message:@"Your CCT must be between 475 and 650"
							  delegate:nil
							  cancelButtonTitle:@"OK"
							  otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
	}
	
	else if (textField == txtUserName4)
	{
		NSString *userNameFour = txtUserName4.text;	
		float numFour = [userNameFour intValue];
		
		if (numFour < .50 || numFour > 3.00)
		{
		
			UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"PSD Error"
							  message:@"Your PSD must be between .50 and 3.00"
							  delegate:nil
							  cancelButtonTitle:@"OK"
							  otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
	}
	
	else if (textField == txtUserName5)
	{
		NSString *userNameFive = txtUserName5.text;
		float numFive = [userNameFive intValue];

		if (numFive < 0.80)
		{
			
			UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"C/D Error"
							  message:@"Your C/D must be between 0 and .80"
							  delegate:nil
							  cancelButtonTitle:@"OK"
							  otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
	}
}
zacharyrs is offline   Reply With Quote
Old 09-11-2011, 08:20 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 1
glennmd is on a distinguished road
Default Validating multiple fields...this works great

6 fields"

t1.text
t2.text
t3.text
diam1.text
diam2.text
dial3.text

t1,2,3 must have string of 8 to be later formatted as date i.e. mm/dd/yy

diam1,2,3 must not be zero

Here is the code using Apple's tag method. Note other simple methods will get repeated calls to the Alert that are unacceptable.

/////Start here//////////////Here Goes://////////////////////
//
// AViewController.m
// A
//
// Created by Glenn Tisman on 8/29/11.
// Copyright 2011 Glenn Tisman, M.D. Inc. All rights reserved.
//

#import "AViewController.h"

@implementation AViewController
@synthesize t1;
@synthesize t2;
@synthesize t3;
@synthesize diam1;
@synthesize diam2;
@synthesize diam3;
@synthesize TVDT;
@synthesize SGR;
@synthesize V0;
@synthesize V1;
@synthesize Vrx;
@synthesize VP;
@synthesize RxResponse;
@synthesize t2t1;
@synthesize t3t1;

/////////////////////////////////////////////////////////////////////////

enum {
t1Tag = 0,
t2Tag,
t3Tag,
diam1Tag,
diam2Tag,
diam3Tag,
};//you must go to assign each field manually through the UIView (find that by clicking on the field to tag and checking the UIView for the place to assign each field with a Tag number. //I suspect that this will work with the : -(BOOL) textFieldShouldEndEditing but I have not tried that.

-(void) textFieldDidEndEditingUITextField *)textField {

switch (textField.tag) {
case 0:
if ([t1.text length] !=8){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yikes" message:@"Check date one"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
case 1:
if ([t2.text length] !=8){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yikes" message:@"Check date two!"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
case 2:
if ([t3.text length] !=8){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yikes" message:@"Check date three!"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;

case 3:
if ([diam1.text length] ==0){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yikes" message:@"Diameter at diagnosis is blank"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
case 4:
if ([diam2.text length] ==0){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yikes" message:@"Diameter at start of Rx is blank"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
case 5:
if ([diam3.text length] ==0){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yikes" message:@"Diameter post Rx is blank"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;

}
glennmd is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 327
12 members and 315 guests
2Apps1Day, akacaj, Domele, Duncan C, GraffitiCircus, michelle, NetGuru, NSString, Paul Slocum, Sloshmonster, soohyun, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,886
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:56 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0