Iphone UITextField only integer

前端 未结 9 575
借酒劲吻你
借酒劲吻你 2020-12-03 03:19

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

相关标签:
9条回答
  • 2020-12-03 03:55

    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:@""];
    }
    
    0 讨论(0)
  • 2020-12-03 03:56

    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;
    }
    
    0 讨论(0)
  • 2020-12-03 04:02

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-03 04:06

    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;
    
        }
    
    0 讨论(0)
  • 2020-12-03 04:08

    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;
    }
    
    0 讨论(0)
  • 2020-12-03 04:09

    http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Objective-C

    Or you could ensure that only the numeric keyboard appears when the focus comes on the field

    0 讨论(0)
提交回复
热议问题