Phone number formatting on iOS
This will help you
Format (xxx) xxx-xxxx
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ int length = (int)[self getLength:textField.text]; //NSLog(@"Length = %d ",length); if(length == 10) { if(range.length == 0) return NO; } if(length == 3) { NSString *num = [self formatNumber:textField.text]; textField.text = [NSString stringWithFormat:@"(%@) ",num]; if(range.length > 0) textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]]; } else if(length == 6) { NSString *num = [self formatNumber:textField.text]; //NSLog(@"%@",[num substringToIndex:3]); //NSLog(@"%@",[num substringFromIndex:3]); textField.text = [NSString stringWithFormat:@"(%@) %@-",[num substringToIndex:3],[num substringFromIndex:3]]; if(range.length > 0) textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]]; } return YES;}- (NSString *)formatNumber:(NSString *)mobileNumber{ mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""]; NSLog(@"%@", mobileNumber); int length = (int)[mobileNumber length]; if(length > 10) { mobileNumber = [mobileNumber substringFromIndex: length-10]; NSLog(@"%@", mobileNumber); } return mobileNumber;}- (int)getLength:(NSString *)mobileNumber{ mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""]; int length = (int)[mobileNumber length]; return length;}
This felt more clear and handles removing any unwanted characters much more nicely. Formats correctly for 1 (###) ###‑#### or (###) ###‑####
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]; NSString *decimalString = [components componentsJoinedByString:@""]; NSUInteger length = decimalString.length; BOOL hasLeadingOne = length > 0 && [decimalString characterAtIndex:0] == '1'; if (length == 0 || (length > 10 && !hasLeadingOne) || (length > 11)) { textField.text = decimalString; return NO; } NSUInteger index = 0; NSMutableString *formattedString = [NSMutableString string]; if (hasLeadingOne) { [formattedString appendString:@"1 "]; index += 1; } if (length - index > 3) { NSString *areaCode = [decimalString substringWithRange:NSMakeRange(index, 3)]; [formattedString appendFormat:@"(%@) ",areaCode]; index += 3; } if (length - index > 3) { NSString *prefix = [decimalString substringWithRange:NSMakeRange(index, 3)]; [formattedString appendFormat:@"%@-",prefix]; index += 3; } NSString *remainder = [decimalString substringFromIndex:index]; [formattedString appendString:remainder]; textField.text = formattedString; return NO;}
The code bellow is what I typically use. The format is different but you get the picture. This will handle input such as '123df#$@$gdfg45-+678dfg901' and output '1 (234) 567-8901'
#import "NSString+phoneNumber.h"@implementation NSString (phoneNumber)-(NSString*) phoneNumber{ static NSCharacterSet* set = nil; if (set == nil){ set = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; } NSString* phoneString = [[self componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@""]; switch (phoneString.length) { case 7: return [NSString stringWithFormat:@"%@-%@", [phoneString substringToIndex:3], [phoneString substringFromIndex:3]]; case 10: return [NSString stringWithFormat:@"(%@) %@-%@", [phoneString substringToIndex:3], [phoneString substringWithRange:NSMakeRange(3, 3)],[phoneString substringFromIndex:6]]; case 11: return [NSString stringWithFormat:@"%@ (%@) %@-%@", [phoneString substringToIndex:1], [phoneString substringWithRange:NSMakeRange(1, 3)], [phoneString substringWithRange:NSMakeRange(4, 3)], [phoneString substringFromIndex:7]]; case 12: return [NSString stringWithFormat:@"+%@ (%@) %@-%@", [phoneString substringToIndex:2], [phoneString substringWithRange:NSMakeRange(2, 3)], [phoneString substringWithRange:NSMakeRange(5, 3)], [phoneString substringFromIndex:8]]; default: return nil; }}@end