Real-Time Validation in a NSTextField.
Real-Time Validation in a NSTextField.
- Subject: Real-Time Validation in a NSTextField.
- From: Abhinay Kartik Reddyreddy <email@hidden>
- Date: Thu, 18 Feb 2010 17:18:31 -0500
I have a NSTextField which should only accept numbers in a certain range say (-72000.00 to 72000.00).
I tried using NSFormatter whihc worked partially. when i hit any alphabet character on the keyboard, the value in the NSTextField stays numeric but the cursor moves forward which is not desired. Heres how i set the formatter.
NSNumberFormatter* numFormatr = [[NSNumberFormatter alloc] init];
[numFormatr setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numFormatr setNumberStyle:NSNumberFormatterDecimalStyle];
[numFormatr setMinimumFractionDigits:2];
[mtextfield setFormatter:numFormatr];
Am i doing something wrong here..??
I tried an alternate approach using the delegate method controlTextDidChange: I ended up with the same problem as above. cursor position is incremented when an invalid character is typed.
- (void) controlTextDidChange:(NSNotification *)obj
{
// get the string value from the textfield
// eliminate invalid characters in the string
// set the result string as stringvalue of textfield
}
I was wondering if there is any alternative / better approach for validating text in a textfield??
Thanks,
Abhinay.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden