iOSManiac iOSManiac - 4 months ago 24x
Objective-C Question

Restricting the keyboard to have numbers only in iPad

In my application, I have different types of inputs which are alphanumeric, numbers ,email etc for UITextField.In iPhone I can successfully use the needed keyboards.But with the same code , iPad fails to restrict numbers in keypad when we use UIKeyboardTypeNumbersAndPunctuations/UIKeyboardTypeDecimalPad.Could you please tell me how I can achieve this in iPad by using objective C.

Thank you very much in advance...


I finally ended up with the following solution. Here I am restricting the keyboard from receiving only numeric input.

if(textField.keyboardType == UIKeyboardTypeNumbersAndPunctuation)
        NSString *validRegEx =@"^[0-9.]*$"; //change this regular expression as your requirement
        NSPredicate *regExPredicate =[NSPredicate predicateWithFormat:@"SELF MATCHES %@", validRegEx];
        BOOL myStringMatchesRegEx = [regExPredicate evaluateWithObject:string];
        if (myStringMatchesRegEx)
            return YES;
            return NO;

Got this code from Stack overflow only.Thanks a lot for this piece of code. Couldn't find the link again .