I am having the problem in validating phone number like (999) 855-6666
How can I validate phone number which is having (xxx) xxx-xxxx
format in iOS?

- 15,750
- 31
- 68
- 83

- 1,193
- 2
- 9
- 20
-
trace the location of (, ) and - . Then match the location from the input String. (Add numerical validation). – Muruganandham K Feb 20 '14 at 13:03
-
@GuillaumeAlgis I think it's not duplicate of what you've mentioned but related with that, not exactly. – Mani Feb 20 '14 at 13:15
-
@iMani I agree. TBH, I ended up flagging the question for not showing minimal understanding. – Guillaume Algis Feb 20 '14 at 13:56
4 Answers
Try this using regular expression. If your string is in this format (xxx)xxx-xxxx, it will get true. Otherwise get failed, even it has space.
NSString *phoneRegex = @"^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
BOOL phoneValidates = [phoneTest evaluateWithObject:phoneNumber];
I have solved with following code. I can do validation with following code.
NSString *phoneRegex2 = @"^(\\([0-9]{3})\\) [0-9]{3}-[0-9]{4}$";
NSPredicate *phoneTest2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex2];
BOOL isValid =[phoneTest2 evaluateWithObject:strNumber];
See regular Expression.

- 1,193
- 2
- 9
- 20
- (NSString *) formatPhone: (NSString *) unformattedString
{
NSString * returnString = [self unformatPhonenumber:unformattedString];
NSCharacterSet* nonNumbers = [[NSCharacterSet
characterSetWithCharactersInString:@"01234567890"] invertedSet];
NSRange r = [unformattedString rangeOfCharacterFromSet: nonNumbers];
if (r.location == NSNotFound)
{
NSRange areaCode = {0, 3};
NSRange prefix = {3, 3};
NSRange suffix = {6, 4};
if ((unformattedString.length == 10) && (! [unformattedString hasPrefix:@"1"]))
{
returnString = [NSString stringWithFormat:@"(%@) %@-%@", [unformattedString substringWithRange:areaCode], [unformattedString substringWithRange:prefix],[unformattedString substringWithRange:suffix]];
}
}
return returnString;
}
- (NSString *) unformatPhonenumber:(NSString *)phoneNumber
{
NSString *phNumber = phoneNumber;
if (phNumber)
{
phNumber = [phNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
phNumber = [phNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
phNumber = [phNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
phNumber = [phNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
}
BOOL valid;
NSCharacterSet *alphaNums = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:phNumber];
valid = [alphaNums isSupersetOfSet:inStringSet];
if (!valid) //If Not numeric return same
return phoneNumber;
else
return phNumber;
}
go through its... if u have any doubt then please post..
Phone number are best validated by confirming that they exist in the AddressBook. That way the Apple-internal locale handling of phone numbers is employed as well as actual usage (it is in the User's address book after all).
If you can't have your needed phone numbers input through AddressBook/contacts then perhaps you can use UIDataDetectorTypePhoneNumber
with a UITextView. See Apple's 'Detecting phone numbers and links in an iPhone application' (However, it doesn't seem Apple provides an interface to get at a detected phone number in a UITextView or even confirm that one was found.)

- 67,920
- 20
- 95
- 145