-3

I have a TextField and wanted to know if the user just pressed numbers

eg:: _tfNumber.text only has numbers?

is there any function on NSString for this?

Pedro
  • 487
  • 1
  • 5
  • 18

5 Answers5

3

try this:

NSCharacterSet *_NumericOnly = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *myStringSet = [NSCharacterSet characterSetWithCharactersInString:mystring];

if ([_NumericOnly isSupersetOfSet: myStringSet]) {
    NSLog(@"String has only numbers");    
}

I got it from: http://i-software-developers.com/2013/07/01/check-if-nsstring-contains-only-numbers/

You can use this method in your UITextField's delegate method textField:shouldChangeCharactersInRange:replacementString: and do the verification while the user is typing.

P. Sami
  • 2,115
  • 2
  • 21
  • 39
  • 1
    +1, because it's a creative solution, but keep in mind that this will give a false positive for the empty string, because any set containing anything is a superset of the empty set. Also, will `isSupersetOfSet:` return `YES` if the two sets are identical, or does the superset actually have to contain additional characters? Depending on that, it may return `NO` for a string containing all of the numbers. – Gavin Mar 17 '14 at 15:17
  • I'm gonna try it in a while and make sure :) thanks @Gavin for the heads up. – P. Sami Mar 17 '14 at 15:26
3

This will let you know if all of the characters are numbers:

NSString *originalString = @"1234";
NSCharacterSet *numberSet = [NSCharacterSet decimalDigitCharacterSet];
NSString * trimmedString = [originalString stringByTrimmingCharactersInSet:numberSet];
if ((trimmedString.length == 0) && (originalString.length > 0)) {
    NSLog(@"Original string was all numbers.");
}

Note that this ensures it won't give a false positive for the empty string, which technically also doesn't contain any non-numbers.

Gavin
  • 8,204
  • 3
  • 32
  • 42
0

No, but it should be easy to write:

- (BOOL)justContainsNumbers:(NSString *)str {
    if ([str length] == 0)
        return NO;
    for (NSUInteger i = 0; i < [str length]; i++)
        if (!isdigit([str characterAtIndex:i]))
            return NO;
    return YES;
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
0

Let's try regular Expression,

NSString * numberReg = @"[0-9]";

NSPredicate * numberCheck = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberReg];

if ([numberCheck evaluateWithObject:textField.text])
    NSLog (@"Number");
Gavin
  • 8,204
  • 3
  • 32
  • 42
Natarajan
  • 3,241
  • 3
  • 17
  • 34
  • Won't this only evaluate true if it matches one number? Your regular expression doesn't match the beginning and end of the string, and also doesn't have repetition. – Gavin Mar 17 '14 at 15:19
  • In fact I confirmed that this only matches when it's a single digit. So it will match the string "1", but not "1234". – Gavin Mar 17 '14 at 15:26
-2

No. NSString is not an NSNumber and any values you get from a UITextField will be an NSString. See THIS SO answer for converting that entered NSString value into an NSNumber.

Community
  • 1
  • 1
LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
  • This doesn't quite answer the question, though I can see your confusion based on the title alone. – James Webster Mar 17 '14 at 15:08
  • It absolutely does answer the question. If the TextFields value is not a number (all digits), then the NSNumber from the formatter will be nil. This method has the benefit of not only checking, but getting the value as well. Please un-downvote. – LJ Wilson Mar 17 '14 at 15:10
  • No. I stand by my down vote as is. I read the question as: "How can I check if a string contains only digits?" This may exclude characters such as `-`, `e`, `E`, `.` which the linked question would accept. Given the question is a little unclear. – James Webster Mar 17 '14 at 15:20