title: Need to restrict character input of UITextField to two characters in iOS
body: <p>I am working on an app that has multiple UITextFields.  For one UITextField, I have set its delegate to self and am calling the delegate method:</p>
  
  <pre><code>- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  </code></pre>
  
  <p>to do a particular task.  However, I have other UITextFields on the same screen for which I would like to do something totally different, in this case, limit the number of characters entered to just two.  Unfortunately, the only way I have seen online that this is possible is to use the above method to make the restriction.  Is this still possible if I am already using the above method to do something completely different for ANOTHER UITextField, and if so, how?</p>
  
  <p>For the record, here is my current implementation of the delegate method:</p>
  
  <pre><code>- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  
      if([[string stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]]
          isEqualToString:@""])
          return YES;
  
      NSString *previousValue = [[[textField.text stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] stringByReplacingOccurrencesOfString:@"." withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@""];
      string = [string stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
      NSString *modifiedValue = [NSString stringWithFormat:@"%@%@", previousValue, string];
  
      if ([modifiedValue length] == 1) {
  
          modifiedValue = [NSString stringWithFormat:@"0.0%@", string];
  
      }
  
      else if ([modifiedValue length] == 2) {
  
          modifiedValue = [NSString stringWithFormat:@"0.%@%@", previousValue, string];
  
      }
  
      else if ([modifiedValue length] &gt; 2) {
  
          modifiedValue = [NSString stringWithFormat:@"%@.%@",[modifiedValue substringToIndex: modifiedValue.length-2],[modifiedValue substringFromIndex:modifiedValue.length-2]];
  
      }
  
  
      NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
      [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
      NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:modifiedValue];
      modifiedValue = [formatter stringFromNumber:decimal];
      textField.text = modifiedValue;
  
      return NO;
  
  }
  </code></pre>