Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iphone UITextField only integer

I have a UITextField in my IB and I want to check out if the user entered only numbers (no char)and get the integer value.

I get the integer value of the UITextField like that :

int integer = [myUITexrtField.text intValue];

When I put a character ( , ; . ) it return me 0 and I don't know how to detect that it is not only numbers.

How can I do?

like image 707
Raphael Pinto Avatar asked Apr 27 '10 08:04

Raphael Pinto


People also ask

How do you make a text field that only accepts 4 digits or numbers on Iphone?

First change your textFiled's keyBoard type UIKeyboardTypeNumberPad , so your keyboard has only numeric/digit. This will prevent deletion once the string has reached four characters. It will also allow you to paste a larger number in if the text is less than 4 characters long.

How do I limit the number of characters in UITextField?

If you have a UITextField or UITextView and want to stop users typing in more than a certain number of letters, you need to set yourself as the delegate for the control then implement either shouldChangeCharactersIn (for text fields) or shouldChangeTextIn (for text views).

How do you check if a string contains only digits Swift?

To check whether the string contains only numbers, we use the concept of set and CharacterSet. decimalDigits together. For a string to contains only a number, all the characters in that string must be a subset of CharacterSet. decimalDigits .


2 Answers

Implementing shouldChangeCharactersInRange method as below does not allow the user input non-numeric characters.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 
    NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0) || [string isEqualToString:@""];
}

This returns YES if the string is numeric, NO otherwise. the [string isEqualToString@""] is to support the backspace key to delete.

I love this approach because it's clean.

like image 120
aslisabanci Avatar answered Sep 25 '22 09:09

aslisabanci


Be sure to set your text field delegate

Use the following function to ensure user can type in numbers only:

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

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

    NSNumber* candidateNumber;

    NSString* candidateString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    range = NSMakeRange(0, [candidateString length]);

    [numberFormatter getObjectValue:&candidateNumber forString:candidateString range:&range error:nil];

    if (([candidateString length] > 0) && (candidateNumber == nil || range.length < [candidateString length])) {

        return NO;
    }
    else 
    {
        return YES;
    }
}
like image 32
Marichka Avatar answered Sep 24 '22 09:09

Marichka