• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Real-Time Validation in a NSTextField.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Real-Time Validation in a NSTextField.
      • From: James Walker <email@hidden>
  • Prev by Date: Re: Appropriate -setWantsLayer: timing
  • Next by Date: close: before NSWindowWillCloseNotification
  • Previous by thread: Re: Implementing Ken-burns Effect
  • Next by thread: Re: Real-Time Validation in a NSTextField.
  • Index(es):
    • Date
    • Thread