Reputation: 11098
My form has two parts, the first part has a "next" button that takes to another view controller where I fill in the second half of the form then submit.
I have the following fields: 1st form: Name, Age, Since (takes date in dd/mm/yyyy) format, From, Notes. 2nd form: Contacts Name, Email, Phone, Notes.
If the 1st form isn't fully completed and the user clicks the "next" button an alert should show explaining what needs to be corrected on the form.
It would be nice to have an alert show if the user leaves a field and goes into another with errors in the field they left. Even better I can have the field border show up as green if all is ok and red if not.
I would like to make it so only alpha characters can be entered in the name field, date in the format of dd/mm/yyyy in the "since" field and so on.
All my text field code is in the prepareForSegue:
method. Right now I create a new instance of an object and set the properties to the text values of the textfields then pass that object to the next controller. I'm wondering if there's a way to make the text field listen when it is clicked in and detect what is being typed so then it is able to check if what is being typed is against it's rules. I've googled and can't seem to find much.
What is the best way to do this or is there standard to follow?
Upvotes: 1
Views: 305
Reputation: 16660
There is a key-value validation pattern in Cocoa. But it is not called automatically on iOS. You have to do it, whenever you think, that you want to inform your user about input errors.
Probably you do not want to do this after a "next" button is clicked. This is web application style and no good idea from user perspective, if it is possible to validate user input isolated for every input field.
You should use a delegate to the input field (probably one for all) and implement the delegate methods for end editing -textFieldDidEndEditing:
or change -textField:shouldChangeCharactersInRange:replacementString:
.
Upvotes: 2
Reputation: 3444
i dont think there is a predefined solution for this problem. but you have to create your own solution.
here are some example-- like for email validation
-(BOOL)validateEmailWithString:(NSString*)email
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
you can create some by your self. here are some link you for help
Upvotes: 2