user1511791
user1511791

Reputation: 1

UITextField Should accept numbers only in iphone

UITextField have to accept the numbers only by using UITableView.

Upvotes: 0

Views: 1513

Answers (3)

Nuzhat Zari
Nuzhat Zari

Reputation: 3408

You have to perform two steps:

  1. Provide keyboard type as UIKeyboardTypeNumberPad, as mentioned by others.
  2. In

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    

    method check that entered string in numeric or not.You can use following method to check numeric value:

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if([self isNumeric:string])
            return TRUE;
        else
            return FALSE;
    }
    
    -(BOOL)isNumeric:(NSString*)inputString
    {
        NSCharacterSet *cs=[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
        NSString *filtered;
        filtered = [[inputString componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [inputString isEqualToString:filtered];
    }
    

Upvotes: 4

Apurv
Apurv

Reputation: 17186

If you have textFiled available while coding, set keyboardType property of UITextField.

 textField.keyboardType = UIKeyboardTypeNumberPad;

Or you can set the same with in xib file.

Upvotes: 0

superGokuN
superGokuN

Reputation: 1424

In your xib file select the textFiled and in the property list there is a field Keyboard select Number Pad

Upvotes: 1

Related Questions