Number only NSTextField's

  • I found this old thread:

    http://lists.apple.com/archives/cocoa-dev/2002/Sep/msg00112.html

    which suggests applying a NSNumberFormatter to my field.

    I have been able to do this and, while it does seem to only accept
    actually numbers in the field (the focus won't change to another
    control until it is a number), I can still type letters, etc. into the
    field as well.

    I would prefer that it just beep when I enter a letter, etc. and for
    that letter to never appear in the field.

    What is the best way to accomplish this?

    Thank you.
  • Write your own custom formatter, which is subclass of NSFormatter.
    Read the following article exists in your machine:

    file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/
    Conceptual/DataFormatting/index.html

    - Apparao.
    Effigent Inc.

    On Dec 21, 2007, at 10:03 AM, Eric Gorr wrote:

    > I found this old thread:
    >
    > http://lists.apple.com/archives/cocoa-dev/2002/Sep/msg00112.html
    >
    > which suggests applying a NSNumberFormatter to my field.
    >
    > I have been able to do this and, while it does seem to only accept
    > actually numbers in the field (the focus won't change to another
    > control until it is a number), I can still type letters, etc. into
    > the field as well.
    >
    > I would prefer that it just beep when I enter a letter, etc. and
    > for that letter to never appear in the field.
    >
    > What is the best way to accomplish this?
    >
    > Thank you.
    >
  • Thanks for the reply.

    Would it not be possible to subclass NSNumberFormatter and override
    (at least partially) isPartialStringValid?
    I would think I could check the incoming string for non-numbers and if
    found, return NO...if not found, call NSNumberFormatter's
    isPartialStringValid function.

    I would hate to lose (or have to reproduce) some of the nice
    functionality of the NSNumberFormatter...

    It seems the NSNumberFormatter does prevent the user from entering
    letters so long as they first typed a number, but if the field is
    empty to begin with, there doesn't seem to be anything to prevent the
    user from entering a letter....seems like a bug.

    On Dec 20, 2007, at 11:50 PM, Apparao wrote:

    > Write your own custom formatter, which is subclass of NSFormatter.
    > Read the following article exists in your machine:
    >
    > file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/Conceptual/DataFormatting/index.html
    >
    >
    > - Apparao.
    > Effigent Inc.
    >
    >
    > On Dec 21, 2007, at 10:03 AM, Eric Gorr wrote:
    >
    >> I found this old thread:
    >>
    >> http://lists.apple.com/archives/cocoa-dev/2002/Sep/msg00112.html
    >>
    >> which suggests applying a NSNumberFormatter to my field.
    >>
    >> I have been able to do this and, while it does seem to only accept
    >> actually numbers in the field (the focus won't change to another
    >> control until it is a number), I can still type letters, etc. into
    >> the field as well.
    >>
    >> I would prefer that it just beep when I enter a letter, etc. and
    >> for that letter to never appear in the field.
    >>
    >> What is the best way to accomplish this?
    >>
    >> Thank you.
    >>
    >
  • On Dec 20, 2007, at 9:10 PM, Eric Gorr wrote:

    > Thanks for the reply.
    >
    > Would it not be possible to subclass NSNumberFormatter and override
    > (at least partially) isPartialStringValid?
    > I would think I could check the incoming string for non-numbers and
    > if found, return NO...if not found, call NSNumberFormatter's
    > isPartialStringValid function.
    >
    > I would hate to lose (or have to reproduce) some of the nice
    > functionality of the NSNumberFormatter...
    >
    > It seems the NSNumberFormatter does prevent the user from entering
    > letters so long as they first typed a number, but if the field is
    > empty to begin with, there doesn't seem to be anything to prevent
    > the user from entering a letter....seems like a bug.
    >

    isPartialStringValid is the way to go. Google it to find examples.

    >
    > On Dec 20, 2007, at 11:50 PM, Apparao wrote:
    >
    >> Write your own custom formatter, which is subclass of NSFormatter.
    >> Read the following article exists in your machine:
    >>
    >> file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/
    >> Conceptual/DataFormatting/index.html
    >>
    >>
    >> - Apparao.
    >> Effigent Inc.
    >>
    >>
    >> On Dec 21, 2007, at 10:03 AM, Eric Gorr wrote:
    >>
    >>> I found this old thread:
    >>>
    >>> http://lists.apple.com/archives/cocoa-dev/2002/Sep/msg00112.html
    >>>
    >>> which suggests applying a NSNumberFormatter to my field.
    >>>
    >>> I have been able to do this and, while it does seem to only
    >>> accept actually numbers in the field (the focus won't change to
    >>> another control until it is a number), I can still type letters,
    >>> etc. into the field as well.
    >>>
    >>> I would prefer that it just beep when I enter a letter, etc. and
    >>> for that letter to never appear in the field.
    >>>
    >>> What is the best way to accomplish this?
    >>>
    >>> Thank you.
    >>>
    >>


    --
    Blog:  http://www.bagelturf.com/  Photos: http://
    bagelturf.smugmug.com/  Buy my book: http://www.bagelturf.com/
    products/getyourheadaroundaperture/
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today