Formatting phone numbers in a text field

You must Login before you can answer or comment on any questions.

Is there any support for auto-formatting phone numbers as they're being entered into a text field, the way that the Contacts utility does?

I have a text field that's using a phone number keyboard, and if the user is trying to enter an international number or even an 11 digit number, it's not very user-friendly with no formatting.

2 Answers

Accepted Answer

Since there is no standard support for this, the easiest would be override the input of the textField by attaching an listener to the 'change' event, then doing your formatting (like adding a space every 3 chars etc.) and then passing the altered input into the textField.

To retrieve the text you could go two ways. Either make a function that strips the textField of the formatting, or store the raw input value somewhere. The first option is probably safer.

Hope this helps you out.

Cheers, Angus

Thanks Angus, this is exactly what I'm doing at the moment. I was just looking at how the phone number text field works in the Contacts utility, and for international numbers, the number of different cases to handle is somewhat overwhelming.

I was really hoping there'd be existing support for formatting phone numbers, since that seems like a pretty central part of iPhone development, but no luck, I guess.

— answered 3 years ago by Mike Dosey
answer permalink
1 Comment
  • I don't have extensive knowledge of the iOS SDK, but I'm sure something like this would be pretty straight forward. It just seems that the functionality hasn't been brought out into Appcelerator.

    I agree though, it does seem like a basic functionality that should be included.

    Cheers, Angus

    — commented 3 years ago by Angus Peart

Your Answer

Think you can help? Login to answer this question!