Upvote:2

Aprove answer
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > self.maxAlertTextFieldLength) ? NO : YES;
}

Upvote:0

UITextField *textField = [alertView textFieldAtIndex:0];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldMaxLimit:) name:UITextFieldTextDidChangeNotification object:textField];


-(void)textFieldMaxLimit:(NSNotification*)notification
{
  UITextField *textField=(UITextField*)notification.object;

        if ([[textField text] length]>22) //choose your limit for characters
        {
            NSString *lastString=[[textField text] substringToIndex:[[textField text] length] - 1];;
            [textField setText:lastString];
        }
 } 

More Answer related to the Same Query

Upvote:0

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

  BOOL isValid = YES;

  @try
  {
     if(5 < [textField.text length])
     {
        isValid = NO;
     }
  }
  @catch (NSException *exception)
  {
     NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
  }
  @finally
  {
     return isValid;
  }
}

Credit Goes to: stackoverflow.com

Related question with same questions but different answers