0

I have implement one client server base application for ipad (sdk 3.2). I have one field price in which user enter price value.But user can enter some alphabetical text by mistake so i want to check this text is numeric or not then how it possible please give me some idea about that.

Thanks in advance.

ManthanDalal
  • 1,063
  • 2
  • 20
  • 45

2 Answers2

1

Always always always the proper way to convert a string into a number is to use an NSNumberFormatter. Using methods like -floatValue and -intValue can lead to false positives.

Behold:

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

NSString * good = @"42.69";
NSString * bad = @"42.69abc";
NSString * abc = @"abc";
NSString * zero = @"0";

NSLog(@"-[good floatValue]: %f", [good floatValue]);
NSLog(@"-[bad floatValue]: %f", [bad floatValue]);
NSLog(@"-[abc floatValue]: %f", [abc floatValue]);
NSLog(@"-[zero floatValue]: %f", [zero floatValue]);

NSNumber * goodNumber = [nf numberFromString:good];
NSNumber * badNumber = [nf numberFromString:bad];
NSNumber * abcNumber = [nf numberFromString:abc];
NSNumber * zeroNumber = [nf numberFromString:zero];

NSLog(@"good (NSNumber): %@", goodNumber);
NSLog(@"bad (NSNumber): %@", badNumber);
NSLog(@"abc (NSNumber): %@", abcNumber);
NSLog(@"zero (NSNumber): %@", zeroNumber);

[nf release];

This logs:

2011-01-07 09:50:07.881 EmptyFoundation[4895:a0f] -[good floatValue]: 42.689999
2011-01-07 09:50:07.884 EmptyFoundation[4895:a0f] -[bad floatValue]: 42.689999
2011-01-07 09:50:07.885 EmptyFoundation[4895:a0f] -[abc floatValue]: 0.000000
2011-01-07 09:50:07.885 EmptyFoundation[4895:a0f] -[zero floatValue]: 0.000000
2011-01-07 09:50:07.886 EmptyFoundation[4895:a0f] good (NSNumber): 42.69
2011-01-07 09:50:07.887 EmptyFoundation[4895:a0f] bad (NSNumber): (null)
2011-01-07 09:50:07.887 EmptyFoundation[4895:a0f] abc (NSNumber): (null)
2011-01-07 09:50:07.888 EmptyFoundation[4895:a0f] zero (NSNumber): 0

Observations:

  • The -floatValue of a non-numeric string (@"42.69abc" and @"abc") results in a false positive (of 0.000000)
  • If a string is not numeric, NSNumberFormatter will report it as nil

As an added bonus, NSNumberFormatter takes locale into account, so it'll (correctly) recognize "42,69" as numeric, if the current locale has , set as the decimal separator.

TL;DR:

Use NSNumberFormatter.

Community
  • 1
  • 1
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
-1

Try this:

- (BOOL) isNumeric:(NSString *)aoInputString {

    NSCharacterSet *decimalSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];


    NSString *decTrimmed = [aoInputString stringByTrimmingCharactersInSet:decimalSet];

    if (decTrimmed.length > 0) {
        return NO;
    } else {
        return YES;
    } 
}
futureelite7
  • 11,462
  • 10
  • 53
  • 87
  • 1
    @JohnWhite except that it's wrong. This will only recognize integer values. If you're dealing with prices, then you have to account for decimal digits (which this fails to do). – Dave DeLong Jan 07 '11 at 17:41
  • Yeah, I apparently copied a wrong part of my code. Please note this might not account for some other invalid cases, such as 12...9 (Extra decimal points), ... (non-decimal) etc. Thank Apple for making this harder than it really should be. (Regexlite might be a better option if you want a more robust solution). – futureelite7 Jan 10 '11 at 01:42
  • except that Apple has made a way to handle this that is stupidly easy. It's called `NSNumberFormatter`, and it will handle more than any regex you wrote would be able to. – Dave DeLong Jan 10 '11 at 07:49
  • i used this @"0123456789.+- " to check phone number with name – Arash Zeinoddini Aug 05 '13 at 21:29