Format phone numbers | Community
Skip to main content
Level 2
October 5, 2011
Solved

Format phone numbers

  • October 5, 2011
  • 6 replies
  • 6811 views

I'm looking to format phone numbers in a text field to xxx-xxx-xxxx, when you tab out of the field. I have users that enter phone numbers many ways 6052222222,605-222-2222,605.222.2222

I Have (this.value = this.value.replace(/[^\*]/g,'*')"/>) on my exit on the field which works fine if your enter the phone number 6052222222. Any direction in the right way would be great, a newbie to this

Thanks,

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Srini_Dhulipall

As Jono Moore mentioned you can control the display using the patterns.

Display Pattern: text{999-999-9999}

Edit Pattern: text{9999999999}|text{999.999.9999}|text{999-999-9999}

But when you want to store the data to DB, the text field will store only the numbers without the formatting. So when you want to display the phone number from the database, the field will know how to format it back to xxx-xxx-xxxx.

Workaround:

But if your intention is to store the phone number as xxx-xxx-xxxx into the DB, then place a hidden field on the form and bind that field to your DB field

On the exit event of the phone number field assign the formatted value to the hidden field.

Donot put any pattern on the hidden field.

Insted of storing the data from the phone number field, store the value of the hidden field to your database.

In the exit event of the phone number field write:

HiddenField.rawValue = PhoneNumberField.formattedValue;

Thanks

Srini

6 replies

Jono_Moore
Level 10
October 5, 2011

Have you tried using the field patterns? You can do most of this with patterns.

Set the Display pattern to "text{999-999-9999}" and you can add all the variations you want for entering phone numbers in the Edit patterns.

hook_emAuthor
Level 2
October 5, 2011

Yes i messed around with the display pattern, problem I'm running into is it doesn't return the correct format to the DB.

Jono_Moore
Level 10
October 5, 2011

I haven't really done anything with databases but you've set the Data pattern as well? That's supposed to control how the data looks on export.

hook_emAuthor
Level 2
October 5, 2011

yes i set the Data pattern as well still not correct. Just looking for some other options

Srini_Dhulipall
Srini_DhulipallAccepted solution
Level 10
October 5, 2011

As Jono Moore mentioned you can control the display using the patterns.

Display Pattern: text{999-999-9999}

Edit Pattern: text{9999999999}|text{999.999.9999}|text{999-999-9999}

But when you want to store the data to DB, the text field will store only the numbers without the formatting. So when you want to display the phone number from the database, the field will know how to format it back to xxx-xxx-xxxx.

Workaround:

But if your intention is to store the phone number as xxx-xxx-xxxx into the DB, then place a hidden field on the form and bind that field to your DB field

On the exit event of the phone number field assign the formatted value to the hidden field.

Donot put any pattern on the hidden field.

Insted of storing the data from the phone number field, store the value of the hidden field to your database.

In the exit event of the phone number field write:

HiddenField.rawValue = PhoneNumberField.formattedValue;

Thanks

Srini

hook_emAuthor
Level 2
October 5, 2011

Thanks Srini