4

I have two TextFields. One accepts only numerical values and the other hex values. I am using NSNumberformatter to set the numerical-only input, like:

NSNumberFormatter *formatter;
formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setNumberStyle:NSNumberFormatterNoStyle];

And then apply it to the TextField.

How do I do the same but only accepting Hex values? By hex I mean 1234567890ABCDEF.

Alternatively, if it cannot be done, how do I check that the text on that TextField is hex?

Thanks

Mr Aleph
  • 1,887
  • 5
  • 28
  • 44

3 Answers3

3

Previous answers explain how to do it using notifications, and link to a question showing how to use key-value validation when binding is involved. Yet another approach is to write a subclass of NSFormatter. Depending on how you write it, you can validate when the user tries to exit the field, or reject invalid characters immediately.

Edit to add: one way to check whether a string is hex:

NSCharacterSet* nonHex = [[NSCharacterSet
  characterSetWithCharactersInString: @"0123456789ABCDEFabcdef"]
  invertedSet];
NSRange nonHexRange = [aString rangeOfCharacterFromSet: nonHex];
BOOL isHex = (nonHexRange.location == NSNotFound);
JWWalker
  • 22,385
  • 6
  • 55
  • 76
  • and yet I am still failing to check whether this is a hex string or not. I'm not going to delete characters, that's plain annoying. I have a notification set already but I have no idea how to check whether a string is hex or not in objective-c. objective-c has all these string method and I might be missing one. I can check char by char but i'm sure there's a better way, as I stated in the question `Alternatively, if it cannot be done, how do I check that the text on that TextField is hex?` – Mr Aleph Oct 06 '11 at 12:40
1

See this answer for a much better explanation, but it would be something like:

- (void)controlTextDidChange:(NSNotification *)aNotification {
    NSError *outError;
    NSControl *textField = [aNotification object];
    NSString *myText = [textField stringValue];

    // check if myText is 0-9 or a-f, do something with it if its not hex.

    // update the NSNextField with the validated text
    [postingObject setStringValue:myText];
}
Community
  • 1
  • 1
chown
  • 51,908
  • 16
  • 134
  • 170
  • Could you explain please? UITextField is undefined and how should I call this? Thank you. – Mr Aleph Oct 05 '11 at 20:59
  • Updated answer, see the [SO question link](http://stackoverflow.com/questions/527322/binding-nstextfield-to-nsnumber/527466#527466) for a much better explanation. – chown Oct 05 '11 at 21:09
0
     - (void)textDidChange:(NSNotification *)aNotification
    {
    //ask the notification for it's sender to return the field object, ask the text field for it's string, if the last char of the string is not valid, delete it and update the string of the field.
//Optionally play the "bell"(alert sound).
    }
awiebe
  • 3,758
  • 4
  • 22
  • 33
  • Thank you but deleting characters like that might be annoying. The way the formatter works is that it won't let you out of the control until you enter only numbers. This way you describe might confuse the user. – Mr Aleph Oct 05 '11 at 20:54