Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This will help you</p> <p>Format (xxx) xxx-xxxx</p> <pre><code>- (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 &gt; 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 &gt; 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 &gt; 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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload