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?
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.
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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With