views:

330

answers:

3

I want to limit the character users can type in iPhone's keyboard, so I created an array of my own. e.g. The array including 0~9 and a dot to enable users to type a price. Then I can return NO for -(BOOL)textField:shouldChangeCharactersInRange:replacementString: if the replace string is not in the array.

The problem is that the backspace button is also disabled when I use this array to filter text. Any ideas about how to enable backspace button?

Another problem is that I want to let users type their names and therefore I don't want to let them switch to numbers and punctuaction (backspace button is also locked if I use an array to filter). How to disable the switch button on the keyboard (Now I just limit them to type a~z, blank and "." , but I think disable the switch button might be a better way)?

+1  A: 

I find a way (maybe not good enough, but it can do the work for backspace function):

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField == txtChargeAmt)
    {
     if(string.length == 0) //backspace button is pressed
     {
      textField.text = [textField.text substringToIndex:(textField.text.length - 1)];
      return NO;
     }

     for(NSString *s in arrNumberAndDot)
     {
      if([string isEqualToString:s])
      {
       return YES;
      }
     }
     return NO;
    }
    else
     return YES;
}

Other ideas about the backspace issue are welcomed. And how to disable the switch button then?

iPhoney
A: 

I guess I am not sure why you would want to use the same TextField for these two different types of input.

I would have two fields, an alphanumeric field for name entry and a numeric field for number entry.

Or am I not getting your question?

mahboudz
Nevermind. I see APple's numeric keypad is really the same as a phone keypad. Same layout and no decimal point. Why?
mahboudz
I'm not using the same TextField. As you can see, I use txtChargeAmt to input price and other textfields may allow me to input other strings like first name/last name.
iPhoney
A: 

This will do what you want a little more succinctly (and efficiently):

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
    NSCharacterSet *validCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];

    BOOL shouldChange = 
     [string length] == 0 || // deletion
     textField != txtChargeAmt || // not the field we care about
     [string rangeOfCharacterFromSet:validCharacterSet].location != NSNotFound;

    if (!shouldChange)
    {
     // Tell the user they did something wrong.  There's no NSBeep() 
     // on the iPhone :(
    }

    return shouldChange;
}

I'd construct that character set somewhere else so you you'd only have to do it once, but you get the idea. Anyone have any thoughts on what to do to alert the user they used an invalid character? I'm trying to solve a similar problem.

Doug McBride
You could use CoreAnimation to quickly shake the textField, like how the Mac OS X login window does when it rejects your password. Not sure how great of an idea it is, but it's a thought.
jbrennan