2016-02-19 14:48:25 +01:00

62 lines
1.9 KiB
Objective-C

//
// NSString+Helper.m
//
//
// Created by Giuseppe Nucifora on 02/07/15.
// Copyright (c) 2015 Giuseppe Nucifora All rights reserved.
//
#import "NSString+Helper.h"
@implementation NSString (Helper)
- (BOOL) isValidEmail
{
BOOL stricterFilter = YES;
NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,15}";
NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:self];
}
- (BOOL)isNumeric {
BOOL isValid = NO;
NSCharacterSet *alphaNumbersSet = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:self];
isValid = [alphaNumbersSet isSupersetOfSet:stringSet];
return isValid;
}
- (BOOL) isValidPhoneNumber {
NSError *error = NULL;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];
NSArray *matches = [detector matchesInString:self options:0 range:NSMakeRange(0, [self length])];
if (matches != nil) {
for (NSTextCheckingResult *match in matches) {
if ([match resultType] == NSTextCheckingTypePhoneNumber) {
return YES;
}
}
}
return NO;
}
- (BOOL) isValidUrl {
NSString *urlRegEx =
@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:self];
}
- (BOOL) isValidTaxCode {
NSString *urlRegEx = @"^[A-Z]{6}[A-Z0-9]{2}[A-Z][A-Z0-9]{2}[A-Z][A-Z0-9]{3}[A-Z]$";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:self];
}
@end