Created
May 9, 2014 20:23
-
-
Save PavelGnatyuk/ab117d8e70029592e3dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (BOOL)isPhoneValid:(NSString *)phone | |
{ | |
NSCharacterSet *phoneSet = [NSCharacterSet characterSetWithCharactersInString:@"-+0123456789()"]; | |
return [[phone stringByTrimmingCharactersInSet:phoneSet] isEqualToString:@""]; | |
} | |
- (BOOL)isPhoneNumberValid:(NSString *)phone | |
{ | |
NSError *error; | |
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error]; | |
NSRange range = NSMakeRange(0, [phone length]); | |
NSArray *matches = [detector matchesInString:phone options:0 range:range]; | |
if ([matches count] == 0) { | |
return NO; | |
} | |
NSTextCheckingResult *result = (NSTextCheckingResult *)[matches firstObject]; | |
return ([result resultType] == NSTextCheckingTypePhoneNumber && result.range.location == range.location && result.range.length == range.length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment