Whats the best way to authenticate and check the strings that a user enters in a UITextField?
My workflow is as follows: 1) user launched view 2) view contains several UITexFields 3) user fills in data inside UIText fields 4) user clicks "save all" button 5) The strings inside the UITextFields are authenticated to see if they are acceptable 6) Activity Indicator is ongoing while authentication is taking place 7) Upon success view is popped off and info issaved 8) if unsuccessful, a UIActionsheet pops up 开发者_高级运维and informs user which fields are incorrect and what the acceptable parameters are.
My question is this: Do I use regular expressions to authenticate the strings? If so are there any helpful tutorials or links for this purpose?
Here is the code:
-(IBAction)saveAllButtonPressed
{
//Authenticate input ie: strings here
//TO BE IMPLEMENTED
if(!passedAuthentication)
{
//show UIActionsheet here
}
else //If passed authentication save everything and pop off the view
{
//sanitize input before storing in customer info model ie. take out dashes, strings etc..
//TO BE IMPLEMENTED
Cart.customerInfoObtained = YES;
infoModel = [[customerInfoModel alloc] initWithObjects:self.Name.text
AptNo:self.AptNum.text
Street1:self.Street1.text
Street2:self.Street2.text
City:self.City.text
Tel:self.Telephone.text
Email:self.Email1.text];
[Cart addCustomerInfo:infoModel];
}
}
I'm not sure what you are trying to authenticate in the text, but with iOS 4.0, there's a class for regular expressions in Objective C (documentation). Depending on what you are looking for when you authenticate regex may or may not be the best way. For example, if you want to make sure the string only has alphanumeric characters, you could do:
NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
BOOL valid = [[MY_STRING stringByTrimmingCharactersInSet:alphaSet] isEqualToString:@""];
This method checks the string and removes all characters found in the NSCharacterSet- then if the string produced has no characters, there were no unexpected characters. You can create your own NSCharacterSets so this might be an easier way to validate compared to regular expressions.
精彩评论