Skip to content

Instantly share code, notes, and snippets.

@PavelGnatyuk
Created May 9, 2014 20:23
Show Gist options
  • Save PavelGnatyuk/ab117d8e70029592e3dd to your computer and use it in GitHub Desktop.
Save PavelGnatyuk/ab117d8e70029592e3dd to your computer and use it in GitHub Desktop.
- (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