Formatting Phone number in Swift
Manipulations with characters in String are not very straightforward. You need following:
Swift 2.1
let s = "05554446677"
let s2 = String(format: "%@ (%@) %@ %@ %@", s.substringToIndex(s.startIndex.advancedBy(1)),
s.substringWithRange(s.startIndex.advancedBy(1) ... s.startIndex.advancedBy(3)),
s.substringWithRange(s.startIndex.advancedBy(4) ... s.startIndex.advancedBy(6)),
s.substringWithRange(s.startIndex.advancedBy(7) ... s.startIndex.advancedBy(8)),
s.substringWithRange(s.startIndex.advancedBy(9) ... s.startIndex.advancedBy(10))
)
Swift 2.0
let s = "05554446677"
let s2 = String(format: "%@ (%@) %@ %@ %@", s.substringToIndex(advance(s.startIndex, 1)),
s.substringWithRange(advance(s.startIndex, 1) ... advance(s.startIndex, 3)),
s.substringWithRange(advance(s.startIndex, 4) ... advance(s.startIndex, 6)),
s.substringWithRange(advance(s.startIndex, 7) ... advance(s.startIndex, 8)),
s.substringWithRange(advance(s.startIndex, 9) ... advance(s.startIndex, 10))
)
Code will print0 (555) 444 66 77
Format inputed digits to US phone number to format (###) ###-####
You can validate the input while entering from the user. Use the UITextFieldDelgate
to do so:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let str = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
if textField == txtFieldPhoneNumber{
return checkEnglishPhoneNumberFormat(string, str: str)
}else{
return true
}
}
Function to validate the input:
func checkEnglishPhoneNumberFormat(string: String?, str: String?) -> Bool{
if string == ""{ //BackSpace
return true
}else if str!.characters.count < 3{
if str!.characters.count == 1{
txtFieldPhoneNumber.text = "("
}
}else if str!.characters.count == 5{
txtFieldPhoneNumber.text = txtFieldPhoneNumber.text! + ") "
}else if str!.characters.count == 10{
txtFieldPhoneNumber.text = txtFieldPhoneNumber.text! + "-"
}else if str!.characters.count > 14{
return false
}
return true
}
Hope this helps!
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;
}
Related Topics
Application Executable Is Missing a Required Architecture Armv6
How to Make Uiimagepickercontroller for Camera and Photo Library at the Same Time in Swift
Swift Protocol Get Only Settable
How to Use Networkreachabilitymanager in Alamofire
iOS 9 Uitableview Separators Insets (Significant Left Margin)
How to Calculate the Uilabel Height Dynamically
How to Convert Date Like \/Date(1440156888750-0700)\/ to Something That Swift Can Handle
Ios: Capture Image from Front Facing Camera
Decrease the Width of the Last Line in Multiline Uilabel
iOS 8 Keyboard Hides My Textview
Disable Bitcode for Project and Cocoapods Dependencies with Xcode 7
Open Uisplitviewcontroller to Master View Rather Than Detail
iOS 13 Uibarbuttonitem Not Clickable and Overlapping Uinavigationbars When Using Uisearchcontroller
How to Get HTML Source from Url with Swift
How to Remove Tmp Directory Files of an iOS App
Capture Uiview and Save as Image