How to restrict characters in a UITextField

I have a UITextField with the following restrictions: characters can only be numbers, as in the method below. I also want to limit the number of maximum characters to three digits. How can I change my method for this?

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *aCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"]; for (int i = 0; i < [string length]; i++) { unichar aCharacter = [string characterAtIndex:i]; if ([aCharacterSet characterIsMember:aCharacter]) { return YES; } } NSUInteger newLength = self.textField.text.length + string.length - range.length; return (newLength > 3) ? NO : YES; } 
+7
ios objective-c iphone cocoa-touch uitextfield
source share
7 answers

o Limit the text box using the code below.

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; return !([newString length] > 3); } 
+12
source share

Your code is close. Try:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *aCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"]; for (int i = 0; i < [string length]; i++) { unichar aCharacter = [string characterAtIndex:i]; if (![aCharacterSet characterIsMember:aCharacter]) { return NO; } } NSUInteger newLength = self.textField.text.length + string.length - range.length; return (newLength > 3) ? NO : YES; } 
+4
source share

You can add an observer to UITextFieldTextDidChangeNotification

// add this line to viewDidLoad

 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(limitTextField) name:UITextFieldTextDidChangeNotification object:nil]; //dealloc add this [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil]; 

// method for limiting text characters

 -(void)limitTextField { textField.text = [textField.text substringToIndex:3]; } 

// or set a text field delegate and use this

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if([textField.text length]<=3) return YES; else return NO; } 

and set the keyBoardType text field as UIKeyboardTypeNumberPad to get only numbers. I did not test or compile the code, I just wrote the logic. Hope this helps :)

+2
source share

Setting maximum character limit and numeric check in UITextField

Setting the maximum character limit in UITextField : - We cannot directly set the maximum length in UITextField because its class does not have a max length property, but this can be achieved using the following text field delegation method.

 #define MAX_LENGTH 20 – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSUInteger newLength = [textField.text length] + [string length] – range.length; if(newLength > MAX_LENGTH){ return NO; } else{ return YES; } } 

Before changing the text field, UITextField asks the delegate if the specified text should be changed. The text field has not changed at the moment, so we take its current length and insert the length of the string, minus the length of the range. If this value is too large (more than 20 characters in this example), return NO to prevent the change.

Numeric check in UITextField

 #define NUMBERS_ONLY 0123456789 – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string { if([textField isEqual:txtPhone]) { NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet]; NSString *filteredstring = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; return ([string isEqualToString:filteredstring]); } return TRUE; } 
+2
source share

Try the following:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *aCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:aCharacterSet] componentsJoinedByString:@""]; if ((textField.text.length >2) && (!(string.length == 0))) return NO; else return [string isEqualToString:filtered]; } 
0
source share

declare this way

  #define MAX_LENGTH_FULLNAME 3 #pragma mark - TextField Delegate method - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ if([string length]==0){ return YES; } NSUInteger newLength = [textField.text length] + [string length] - range.length; if ([string isEqualToString:@"\n"]) { [self.txtAnswer resignFirstResponder]; return NO; } if (textField == self.txtFName){ /* for backspace */ if([string length]==0){ return YES; } if (newLength > MAX_LENGTH_FULLNAME) { if([string length] > MAX_LENGTH_FULLNAME){ textField.text = [string substringToIndex:MAX_LENGTH_FULLNAME]; } return NO; } NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"]; for (int i = 0; i < [string length]; i++) { unichar c = [string characterAtIndex:i]; if ([myCharSet characterIsMember:c]) { return YES; } } return NO; } return YES; } 

to allow only numbers that you can also simply define in this way

 [textField setKeyboardType:UIKeyboardTypeNumberPad]; 
0
source share

This will help.

This is a duplicate answer: Go check this Answer Set the maximum length of the UITextField character

0
source share

All Articles