Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField - UIKeyboardTypeDecimalPad on iPad?

On the iPad...

textField.keyboardType = UIKeyboardTypeDecimalPad;

...just shows the regular keyboard, but starting on the numbers at the top (with lots of punctuation underneath).

But it's to type in a number. I only want the users to be able to type numbers and a decimal point, like UIKeyboardTypeDecimalPad does on the iPhone.

Is there any way to make the irrelevant keys go away and leave my user alone?

like image 293
DenverCoder9 Avatar asked Jun 14 '11 21:06

DenverCoder9


3 Answers

Setting a UITextField's keyboardType only makes it easier for a user to enter appropriate characters. Even on the iPhone, users can enter other characters via a hardware keyboard, or by pasting in a string.

Instead, implement UITextFieldDelegate's -textField:shouldChangeCharactersInRange:replacementString: to validate user input. You also probably don't really want to hardcode the digits 0-9 and a period. Users in some locales, for example, separate whole numbers from decimals with a comma:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *candidate = [[textField text] stringByReplacingCharactersInRange:range withString:string];
    if (!candidate || [candidate length] < 1 || [candidate isEqualToString:@""])
    {
        return YES;
    }
    NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:candidate];
    if (!number || [number isEqualToNumber:[NSDecimalNumber notANumber]])
    {
        return NO;
    }
    return YES;
}

Alternately, you might perform validation when the user finishes entering text, in –textFieldShouldReturn:, – textFieldShouldEndEditing:, or – textFieldDidEndEditing: as desired.

like image 161
lemnar Avatar answered Nov 03 '22 19:11

lemnar


This is not necessarily a foolproof solution, but it matched my needs and might be helpful to others. The concern would be that the number chars are hardcoded. I'm not sure of locales that use other numbers chars, but they can be added as needed.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *candidate = [[textField text] stringByReplacingCharactersInRange:range withString:string];

    // Ensure that the local decimal seperator is used max 1 time
    NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
    NSString *decimalSymbol = [formatter decimalSeparator];
    if ([candidate componentsSeparatedByString:decimalSymbol].count > 2) return NO;

    // Ensure that all the characters used are number characters or decimal seperator
    NSString *validChars = [NSString stringWithFormat:@"0123456789%@", decimalSymbol];
    if ([candidate stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:validChars]].length) return NO;

    return YES;
}
like image 2
Dash Avatar answered Nov 03 '22 19:11

Dash


I know this question has been inactive for quite a while, but this might help somebody out there.

You can try implementing this one https://github.com/azu/NumericKeypad. The coder implemented his own number pad buy subclassing the UITextField

like image 1
acecapades Avatar answered Nov 03 '22 18:11

acecapades