single - Iphone UITextField only integer




uitextfield numbers only swift (6)

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?



Answer by @devsan is incorrect. If a user pastes anything with a a number such as "1abc" her code would break

It should be (allow replacement only if all the chars are digits):

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

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 [email protected]""] is to support the backspace key to delete.

I love this approach because it's clean.


This code is work with localisation too .

  - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if([string isEqualToString:@""]) return YES;
        NSCharacterSet *characterSet = nil;
        characterSet = [NSCharacterSet decimalDigitCharacterSet];
        NSRange location = [string rangeOfCharacterFromSet:characterSet];
        return (location.location != NSNotFound);

        if(characterSet == nil) return YES;
        return YES;

    }

Try this: It will stop user to enter any character other then numbers

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];

   if ([string rangeOfCharacterFromSet:nonNumberSet].location != NSNotFound) 
   {
       return NO;
   }
   return YES;
}

Want to allow negative numbers as well?

Elaborating on davsan's answer, here is a solution that supports entering negative as well as positive numbers, and disallows anything else.

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

    return YES;
}




casting