How to set limit in text field? - iphone

can any body tell me how to set the limit for words to enter in a text field in objective-c?
In my case i have a registration form and in registration form every user have to enter username, password, email and phone no. and now i have to restrict the user to enter a name of maximum 15 characters.
can any one tell me how i can do this?

You are able to restrict number of inputs in UITextField both following ways.
First of all, set relevant UIViewController as a delegate object to UITextField
yourTextField.delegate = self;
Then add the following code snippet to your UIViewController.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.text length]<15) {
return YES;
}
return NO;
}
Or you can define UITextFieldDelegate protocol in your UIViewController interface
#interface YourViewController()<UITextFieldDelegate>
// YourViewController.m
#end
and then implement the following shouldChangeCharactersInRange task in your class.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 15) ? NO : YES;
}

Try this:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *textFieldText = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([textFieldText length] > 15)
{
return NO;
}
return YES;
}

Related

UITextField Should accept numbers only in iphone

UITextField have to accept the numbers only by using UITableView.
You have to perform two steps:
Provide keyboard type as UIKeyboardTypeNumberPad, as mentioned by others.
In
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
method check that entered string in numeric or not.You can use following method to check numeric value:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([self isNumeric:string])
return TRUE;
else
return FALSE;
}
-(BOOL)isNumeric:(NSString*)inputString
{
NSCharacterSet *cs=[[NSCharacterSet characterSetWithCharactersInString:#"0123456789"] invertedSet];
NSString *filtered;
filtered = [[inputString componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:#""];
return [inputString isEqualToString:filtered];
}
In your xib file select the textFiled and in the property list there is a field Keyboard select Number Pad
If you have textFiled available while coding, set keyboardType property of UITextField.
textField.keyboardType = UIKeyboardTypeNumberPad;
Or you can set the same with in xib file.

return keyboard on entering one character in textfield

I am developing an iphone application in which i have to return keyboard as soon as i type only one character in textfield. How to achieve this please suggest some solution.
Thanks.
Step 1: Create a class implementing the protocol UITextFieldDelegate
#interface TheDelegateClass : NSObject <UITextFieldDelegate>
Step 2: In your implementation, override the method - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// newString is what the user is trying to input.
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([newString length] < 1) {
// If newString is blank we will just ingore it.
return YES;
} else
{
// Otherwise we cut the length of newString to 1 (if needed) and set it to the textField.
textField.text = [newString length] > 1 ? [newString substringToIndex:1] : newString;
// And make the keyboard disappear.
[textField resignFirstResponder];
// Return NO to not change text again as we've already changed it.
return NO;
}
}
Step 3: Set an instance of the delegate class as the delegate of the UITextField.
TheDelegateClass *theDelegate = [[TheDelegateClass alloc] init];
[theTextField setDelegate:theDelegate];
you have to write your code in text delegate method of
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([textField.text length] == 1){
[textField resignFirstResponder];
}
and then check your length of string in textFieldDidBeginEditing
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if([textField.text length] == 1){
[textField resignFirstResponder];
}
}
add notification in textField creating code
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(changeText:) name:UITextFieldTextDidChangeNotification object:textField];
and implement
- (void) changeText: (id) sender;
{
if ([textField.text length] == 1)
{
[textField resignFirstResponder];
}
}
I guess this is want you are looking for?
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[add your method here];
return YES;
}
Or if you want it to resign as soon as it starts to edit you can put this code in textFieldDidBeginEditing: delegate method
[textField resignFirstResponder];
check this link
https://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([textField.text length] == 1){
[textField resignFirstResponder];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if([textField.text length]==1)
{
// here perform the action you want to do
}
}

return textfield keyboard on editing

I am working on iphone application in which i have to return the keyboard when i edit my textfield or delete the text in textfield also my textfield should allow only one character in textfield.
Thanks
Implement UITextFieldDelegate protocol and implement the following method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
You can get the length of the text in the textfield. If it is greater than 1, then, resign the first responder.
You can do something like this, considering _textField as your textfield object.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if( textField == _textField )
{
if( [textField.text length] == 0 )
{
return YES;
}
else
{
[textField resignFirstResponder];
return NO;
}
}
else
{
return YES;
}
}

How to stop entering value in UITextField

How to stop entering value in UITextField if the length is greater then 20 char.
Programmatically.
try this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//Set tag for a textfield
if(textField.tag==10)
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 20) ? NO : YES;
}
}
Set the maximum character length of a UITextField

IPhone Objective-C only enable a button if NSTextfield is not blank

I have a button that I only want enabled when a textfield is not blank. I have been trying to do this by implementing the following method, without any luck.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
Is there another delegate method that I could implement that gets fired after the text is changed? In this method is there some way to tell what key is being pressed, if it's a delete or something like that?
You can just check for string.length == 0 and range.length > 0;. This denotes a deletion. However, what you really want is to just do the pending modification and then check that you still have a non-empty string.
Something like this (typed in, not compiled):
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *testString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if( testString.length )
myButton.enabled = YES;
else
myButton.enabled = NO;
return YES;
}