I have a TextField in my iPad application. I want the "Number Pad" to come on touch (without the p开发者_Go百科unctuation marks and Alphabet Keyboard Button).How do I do that ? How to validate the TextField ?
NSString *nameRegex =@"[0-9]";
NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex];
BOOL isNumber=[nameTest evaluateWithObject:string];
if(isNumber) {
// valid number found
} else {
// error message
}
Use this validation code in the UITextField delegate method textField:shouldChangeCharactersInRange:replacementString:
to prevent the user from even entering an invalid value.
Set the textField keyboard = NumberPad in your interface builder.
Thanks
Mitesh
You cannot get a number pad on ipad.You yourself have to put a check what the user has entered from keyboard, if its not number show them an alert to enter numbers.
You can also use with askee mumber validation as below I have made validation in which I have consider onley numeric and back button.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if([string isEqualToString:[NSString stringWithFormat:@"%c", 48]]||[string isEqualToString:[NSString stringWithFormat:@"%c", 49]] ||[string isEqualToString:[NSString stringWithFormat:@"%c", 50]] ||[string isEqualToString:[NSString stringWithFormat:@"%c", 51]] ||[string isEqualToString:[NSString stringWithFormat:@"%c", 52]] ||[string isEqualToString:[NSString stringWithFormat:@"%c", 53]] ||[string isEqualToString:[NSString stringWithFormat:@"%c", 54]] ||[string isEqualToString:[NSString stringWithFormat:@"%c", 55]] ||[string isEqualToString:[NSString stringWithFormat:@"%c", 56]] ||[string isEqualToString:[NSString stringWithFormat:@"%c", 57]] || [string isEqualToString:[NSString stringWithFormat:@"%c", 46]]||[string isEqualToString:@""])
{
if(textField==myText)
return YES;
else
return NO;
}
else
return NO;
}
Here's a number pad implementation for the iPad: https://github.com/IngmarStein/NumericKeypad
Disclaimer: it's my own fork of azu's NumericKeypad
精彩评论