iPhone Text Field Validation - In any user based application "validations" plays a very important role in order to get unique and correct data from user.
iPhone Text Field Validation - In any user based application "validations" plays a very important role in order to get unique and correct data from user.In any user based application "validations" plays a very important role in order to get unique and correct data from user. In this tutorial we are going to limiting the text field input length in iPhone app.
Let's find out what we are doing in the application...
Here we have created a view based application and called it "Validate". We have also take a Text field on the view to take the input from user.
Now we have to make sure that the max string length of the text field should be 6. That means user anyhow user should not be able to enter the data that have the string length more then 6.
To do that.. first we need to declare UITextFieldDelegate in .h file and then declare textValue.delegate = self; into viewDidLoad method, in the .m file
After that just create a method BOOL as given below...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string |
It'll validate the text field and it'll not accept more than 6 characters.
See the code below: validateViewController.h
#import <UIKit/UIKit.h> @interface validateViewController : UIViewController < UITextFieldDelegate > { @end |
validateViewController.m
#import "validateViewController.h" @implementation validateViewController - (void)viewDidLoad { - (void)didReceiveMemoryWarning { - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string -(void)touchesBegan :(NSSet *)touches withEvent:(UIEvent *)event - (void)viewDidUnload { - (void)dealloc { @end |
You can also Download the iPhone Text Field Validation code.