How Do I Filter Characters That the User is Entering into a UITextField?

Discussion in 'iOS Programming' started by kaydell.leavitt, Apr 22, 2010.

  1. kaydell.leavitt macrumors regular

    Joined:
    Apr 19, 2010
    #1
    I have several UITextFields in which the user can enter numbers representing money. I want to enable the user to only type in the digits zero through nine and the decimal point (a comma or a period).

    How can I filter-out the characters that aren't allowed to be part of a currency string?

    -- Kaydell :confused:
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Have you looked at the documentation? If so what? You need to learn to find the answers for yourself.
     
  3. kaydell.leavitt thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #3
    Documentation

    Here is the documentation that I'm looking at:

    UITextFieldDelegate Protocol Reference
    http://developer.apple.com/iphone/l.../UITextFieldDelegate/UITextFieldDelegate.html

    UITextField Class Reference
    http://developer.apple.com/iphone/l.../UITextField_Class/Reference/UITextField.html

    I'm thinking that I can make my own custom class that adheres to the UITextFieldDelegate protocol and set it for my text fields and then implement the method:

    Code:
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    
    Am I on the right track?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Yes. Unless there are pre-built formatters available. There are for Mac OSX. I'm not sure about the iPhone OS
     

Share This Page