Helpful Information
 
 
Category: iPhone SDK Development
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:


- (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)
http://screencast.com/t/NWNjZTU2M

Any advice would be greatly appreciated!










privacy (GDPR)