Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Formatting phone numbers in a text field

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.

— asked October 12th 2010 by Mike Dosey
  • field
  • format
  • iphone
  • mobile
  • number
  • phone
  • text

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.


    — answered October 12th 2010 by Angus Peart
  • 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 October 12th 2010 by Mike Dosey
    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.


      — commented October 12th 2010 by Angus Peart
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.