0

Here is the challenge; I want to ensure a number that is entered into a UItextview is 65535 [a short] or less. I can restrict the top digit to

an[NSCharacterSet characterSetWithCharactersInString:@"123456"];

But I need to be smarter, cause the next digit can be any number up to 65; but I cannot simply use a NSCharacterSet or can I?

What are your thoughts?

user3069232
  • 8,587
  • 7
  • 46
  • 87

1 Answers1

0

Ok, Found a solution of sorts which turns out to be simple; I like simple. Used this call & this code.

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

if ([textField.accessibilityIdentifier isEqualToString:@"minor"]) {

  if (newLength == 0) {
    minorMaxColumns = 5;
  }         
  if (newLength == 2) {
    if ([string integerValue] >= 6) {
      minorMaxColumns = 4;
    }
  }

  if (newLength == 3) {
    if ([textField.text integerValue] >= 65) {
       if  ([string integerValue] >= 5) {
         minorMaxColumns = 4;
       }
     }
  }
  if ([self isiBeaconMajorMinor:string]  && (newLength <= minorMaxColumns)) {
    return YES;
  }
return NO;
}

-(BOOL)isiBeaconMajorMinor:(NSString*)inputString{
    BOOL isValid = NO;
    NS CharacterSet *alphaNumbersSet = [NSCharacterSet      decimalDigitCharacterSet];
    NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:inputString];
    isValid = [alphaNumbersSet isSupersetOfSet:stringSet];
   return isValid;
}

Doesn't quite give you 65535, it only does 65499; but is is close enough. Of course your welcome to come up with a better solution. The variable "minorMaxColumns" is also set by the Editing Did Begin Event. The secret of the solution to look at both the last digit and the first and to restrict the length.

user3069232
  • 8,587
  • 7
  • 46
  • 87