Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
i need to enter mobile number in a text field.
i need to display mobile number like this format 123-456-7890.
for eg: 1234567890 is my mobile number,while am entering this mobile number in text field,
for first 3 digits i need to place -,after 3 digits again i need to place -.
if i enter 123 then automatically place - in text field,after 456 place ,no need of placing for further 4 digits.
similar to displaying text in currency format.
but while getting text from that text field i need to get mobile number no need of - like 1234567890,not 123-456-7890.
i think my question is quite clear now,let me add comment if is not.
Thank u in advance.
–
Just to clarify: As a user enters a phone number into a UITextField, you would like it to automatically insert dashes in the proper places.
The answer is in using the UITextFieldDelegate protocol.
1) Set your controller object as a delegate for the UITextField.
2) You'll find the following method in the protocol:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
This method is called every time a character change occurs in the text field.
3) How robust you want your implementation to be is up to you. You could simply do a count of the current characters and insert dashes after 3 and 6 characters. It would be wise to also reject any non-numeric characters.
Here is a sample implementation. We basically take over the field editing manually - Inserting dashes after the appropriate string lengths and making sure the user can only enter numbers:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *numSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789-"];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int charCount = [newString length];
if ([newString rangeOfCharacterFromSet:[numSet invertedSet]].location != NSNotFound
|| [string rangeOfString:@"-"].location != NSNotFound
|| charCount > 12) {
return NO;
if (charCount == 3 || charCount == 7) {
newString = [newString stringByAppendingString:@"-"];
textField.text = newString;
return NO;
–
–
–
Updated Matthew McGoogan's code : This works fine with back space also..
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField.tag == 8) {
NSCharacterSet *numSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789-"];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int charCount = [newString length];
if (charCount == 3 || charCount == 7) {
if ([string isEqualToString:@""]){
return YES;
}else{
newString = [newString stringByAppendingString:@"-"];
if (charCount == 4 || charCount == 8) {
if (![string isEqualToString:@"-"]){
newString = [newString substringToIndex:[newString length]-1];
newString = [newString stringByAppendingString:@"-"];
if ([newString rangeOfCharacterFromSet:[numSet invertedSet]].location != NSNotFound
|| [string rangeOfString:@"-"].location != NSNotFound
|| charCount > 12) {
return NO;
textField.text = newString;
return NO;
return YES;}
This will format as so: (444) 444-4444
It also handles backspaces, unlike the answer above.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField == _txtPhone1 || textField == _txtPhone2 || textField == _txtPhone3 || textField == _txtPhone4)
NSCharacterSet *numSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789-() "];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int charCount = [newString length];
if ([newString rangeOfCharacterFromSet:[numSet invertedSet]].location != NSNotFound
|| [string rangeOfString:@")"].location != NSNotFound
|| [string rangeOfString:@"("].location != NSNotFound
|| [string rangeOfString:@"-"].location != NSNotFound
|| charCount > 14) {
return NO;
if (![string isEqualToString:@""])
if (charCount == 1)
newString = [NSString stringWithFormat:@"(%@", newString];
else if(charCount == 4)
newString = [newString stringByAppendingString:@") "];
else if(charCount == 5)
newString = [NSString stringWithFormat:@"%@) %@", [newString substringToIndex:4], [newString substringFromIndex:4]];
else if(charCount == 6)
newString = [NSString stringWithFormat:@"%@ %@", [newString substringToIndex:5], [newString substringFromIndex:5]];
else if (charCount == 9)
newString = [newString stringByAppendingString:@"-"];
else if(charCount == 10)
newString = [NSString stringWithFormat:@"%@-%@", [newString substringToIndex:9], [newString substringFromIndex:9]];
textField.text = newString;
return NO;
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.