Forum Moderators: open

Message Too Old, No Replies

clean up (standardize) a phone number?

         

partha

10:59 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



how can I get javascript to clean up/standardize a user-entered phone number?
So for example if the user enters one of these:
234-873-2398
2348732398
(234) 873-2398
it reformats to 2348732398, and if an invalid phone number is entered it alrerts the user to check it.

Bernard Marx

11:11 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var no = "234-873-2398"

// remove all non-digit chars
var standardNo = no.replace(/[^\d]/g,'')

alert(standardNo)

if(standardNo.length!= 10)
alert('non-standard number')

Nutter

1:11 am on Aug 5, 2005 (gmt 0)

10+ Year Member



Not intending to hijack (too much :-)), but I've been thinking of adding phone number formatting to a site of mine. The problem I've had is dealing with extensions. Ex: Phone number (123) 555-1212 Extension 123. Somewhere I have some VB code that uses "x" or "X" to split the string to check for an extension and then strips out all non-numeric characters before reformatting that I suppose I should rewrite in JS.

The real question - is there a "standard" way to show an extension? I'm thinking (123) 555-1212 x123 is correct.

One thought about the code above - it requires a 10 digit number. You may want to consider allowing 7 digit numbers and either leaving the area code blank or filling in a local default. Of course it also causes problems with non-US numbers.

Bernard Marx

1:32 am on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I personally have no idea what a 'standard' phone number is in the US. I just assumed it was 10 digits, as given.

To deal with numbers potentially from outside the US, the formats no doubt vary enormously. Many US-based sites have historically excluded their international users with this kind of slip.

The best action may be to not attempt any checking at all.

jalarie

4:22 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



I hope this isn't taken as being at all promotional; I've nothing to sell. I do have a FREE JavaScript routine which takes every U.S.A. telephone number format that I can think of, and gives back whichever format you want. I expect that the code is too long to post here, but I'll try. If it gets whacked off, please send me a message and I'll get it to you somehow. Here goes:

// phonfmt1.js
//
// Call PhonFmt1 with a phone number to be formatted, local Area Code,
// and format-out code. Valid format-out codes are:
//
// format code description example
// ------------------- --------------------------------------------------- -----------------
// ####### seven digits. (default) . . . . . . . . . . . . . 7623394
// #######x? seven digits plus extension . . . . . . . . . . . . 7623394x21
// ###-#### seven digits with separator . . . . . . . . . . . . 762-3394
// ###-####x? seven digits, separator, extension . . . . . . . . 762-3394x21
// ########## ten digits . . . . . . . . . . . . . . . . . . . . 8107623394
// ##########x? ten digits plus extension . . . . . . . . . . . . . 8107623394x21
// ###-###-#### ten digits with separators . . . . . . . . . . . . 810-762-3394
// ###-###-####x? ten digits, separators, extension . . . . . . . . . 810-762-3394x21
// ###-####### ten digits, single separator . . . . . . . . . . . 810-7623394
// ###-#######x? ten digits, single separator, extension . . . . . . 810-7623394x21
// (###)####### ten digits, area code in parens . . . . . . . . . . (810)7623394
// (###)#######x? ten digits, area code in parens, extension . . . . (810)7623394x21
// (###)###-#### ten digits, area code in parens, separator . . . . (810)762-3394
// (###)###-####x? ten digits, a/c in parens, separator, extension . . (810)762-3394x21
// (###) ###-#### ten digits, a/c parens, space, separator . . . . . (810) 762-3394
// (###) ###-####x? ten digits, a/c parens, space, separator, ext. . . (810) 762-3394x21
// 1########## eleven digits . . . . . . . . . . . . . . . . . . . 18107623394
// 1##########x? eleven digits plus extension . . . . . . . . . . . 18107623394x21
// 1-###-###-#### eleven digits with separators . . . . . . . . . . . 1-810-762-3394
// 1-###-###-####x? eleven digits, separators, extension . . . . . . . 1-810-762-3394x21
// 1-###-####### eleven digits, two separators . . . . . . . . . . . 1-810-7623394
// 1-###-#######x? eleven digits, two separators, extension . . . . . 1-810-7623394x21
// 1-########## eleven digits, single separator . . . . . . . . . . 1-8107623394
// 1-##########x? eleven digits, single separator, extension . . . . 1-8107623394x21
// 1(###)####### eleven digits, area code in parens . . . . . . . . 1(810)7623394
// 1(###)#######x? eleven digits, a/c in parens, extension . . . . . . 1(810)7623394x21
// 1(###)###-#### eleven digits, area code in parens, separator . . . 1(810)762-3394
// 1(###)###-####x? eleven digits, a/c in parens, separator, ext. . . . 1(810)762-3394x21
// +1########## plus, 11 digits . . . . . . . . . . . . . . . . . . +18107623394
// +1##########x? plus, 11 digits plus extension . . . . . . . . . . +18107623394x21
// +1-###-###-#### plus, 11 digits with separators . . . . . . . . . . +1-810-762-3394
// +1-###-###-####x? plus, 11 digits, separators, extension . . . . . . +1-810-762-3394x21
// +1-###-####### plus, 11 digits, two separators . . . . . . . . . . +1-810-7623394
// +1-###-#######x? plus, 11 digits, two separators, extension . . . . +1-810-7623394x21
// +1-########## plus, 11 digits, single separator . . . . . . . . . +1-8107623394
// +1-##########x? plus, 11 digits, single separator, extension . . . +1-8107623394x21
// +1(###)####### plus, 11 digits, area code in parens . . . . . . . +1(810)7623394
// +1(###)#######x? plus, 11 digits, a/c in parens, extension . . . . . +1(810)7623394x21
// +1(###)###-#### plus, 11 digits, area code in parens, separator . . +1(810)762-3394
// +1(###)###-####x? plus, 11 digits, a/c in parens, separator, ext. . . +1(810)762-3394x21
//
// Examples:
//
// Phone_out=PhonFmt1(input_phone_number,'810','(###)###-####x?');
//
// Notes:
// All of the above example telephone numbers would be recognized as valid
// NANP (North American Numbering Plan) input. To allow extra dashes and
// other special characters to appear elsewhere in the input number, change
// PH_Strict to 'no' below. Change PH_Null to 'no' to make null inputs
// produce an error message.
// Input separators are not checked.
// Any UPPER-CASE letters will be replaced with the appropriate telephone
// keypad numbers. The letters 'Q' and 'Z' become '1' and '0' respectively.
// Extensions must be marked by a lower-case 'x' to be recognized.
// Output extension formats must have exactly one question (?) mark even though
// the length of the output extension will be the entire entered value.
// If the format-out code has a comma, whatever is after the comma will be used
// in place of the dashes in the output value.
// The default Area Code is three blanks.

function PhonFmt1(IPhone,ACode,FormatO) {
PH_Strict='yes'; // valid NANP input formats only
PH_Null='allow';

PH_ACode=ACode; // default Area Code
PH_IPhone=IPhone; // phone number to format
PH_FormatO=FormatO; // desired output format

if ((!PH_IPhone) && (PH_Null == 'allow')) { return ''; }
if (!PH_ACode) { PH_ACode=' '; }
if (!PH_FormatO) { PH_FormatO='#######'; }
PH_Dash='-';
if ((PH_ix1=PH_FormatO.indexOf(',')) > -1) {
PH_Dash=PH_FormatO.substring(PH_ix1+1);
PH_FormatO=PH_FormatO.substring(0,PH_ix1);
}

PH_IPhone=PH_IPhone.replace(/[ABC]/g,'2');
PH_IPhone=PH_IPhone.replace(/[DEF]/g,'3');
PH_IPhone=PH_IPhone.replace(/[GHI]/g,'4');
PH_IPhone=PH_IPhone.replace(/[JKL]/g,'5');
PH_IPhone=PH_IPhone.replace(/[MNO]/g,'6');
PH_IPhone=PH_IPhone.replace(/[PRS]/g,'7');
PH_IPhone=PH_IPhone.replace(/[TUV]/g,'8');
PH_IPhone=PH_IPhone.replace(/[WXY]/g,'9');
PH_IPhone=PH_IPhone.replace(/Q/g, '1');
PH_IPhone=PH_IPhone.replace(/Z/g, '0');

if (PH_Strict!= 'yes') { // allow non-NANP input formats
PH_IPhone=PH_IPhone.replace(/[\ -\*]/g,''); // printables before +
PH_IPhone=PH_IPhone.replace(/[\,-\/]/g,''); // between + and 0
PH_IPhone=PH_IPhone.replace(/[\:-\@]/g,''); // between 9 and A
PH_IPhone=PH_IPhone.replace(/[\[-\`]/g,''); // between Z and a
PH_IPhone=PH_IPhone.replace(/[\{-\~]/g,''); // printables after z
}

PH_Work=''; PH_Ext='';
if ((PH_ix1=PH_IPhone.indexOf('x')) > -1) {
PH_Ext=PH_IPhone.substring(PH_ix1);
PH_IPhone=PH_IPhone.substring(0,PH_ix1);
}

// seven digits [extension]:
// ex: 7623394 or 7623394x21
if (PH_IPhone.match(/^[2-9][0-9]{6}$/)) {
PH_Work=''+PH_ACode+PH_IPhone;
}
// seven digits with separator [extension]:
// ex: 762-3394 or 762-3394x21
if (PH_IPhone.match(/^[2-9][0-9]{2}.[0-9]{4}$/)) {
PH_Work=''+PH_ACode+PH_IPhone.substring(0,3)+PH_IPhone.substring(4);
}
// ten digits [extension]:
// ex: 8107623394 or 8107623394x21
if (PH_IPhone.match(/^[2-9][0-9]{2}[2-9][0-9]{6}$/)) {
PH_Work=''+PH_IPhone;
}
// ten digits with separators [extension]:
// ex: 810-762-3394 or ex: 810-762-3394x21
if (PH_IPhone.match(/^[2-9][0-9]{2}.[2-9][0-9]{2}.[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(0,3)+PH_IPhone.substring(4,7)+PH_IPhone.substring(8);
}
// ten digits; single separator [extension]:
// ex: 810-7623394 or 810-7623394x21
if (PH_IPhone.match(/^[2-9][0-9]{2}.[2-9][0-9]{2}[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(0,3)+PH_IPhone.substring(4);
}
// ten digits, area code in parens [extension]:
// ex: (810)7623394 or (810)7623394x21
if (PH_IPhone.match(/^.[2-9][0-9]{2}.[2-9][0-9]{2}[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(1,4)+PH_IPhone.substring(5);
}
// ten digits, area code in parens, separator [extension]:
// ex: (810)762-3394 or (810)762-3394x21
if (PH_IPhone.match(/^.[2-9][0-9]{2}.[2-9][0-9]{2}.[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(1,4)+PH_IPhone.substring(5,8)+PH_IPhone.substring(9);
}
// ten digits, a/c parens, space, separator [extension]:
// ex: (810) 762-3394 or (810) 762-3394x21
if (PH_IPhone.match(/^.[2-9][0-9]{2}.{2}[2-9][0-9]{2}.[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(1,4)+PH_IPhone.substring(6,9)+PH_IPhone.substring(10);
}
// eleven digits [extension]:
// ex: 18107623394 or 18107623394x21
if (PH_IPhone.match(/^1[2-9][0-9]{2}[2-9][0-9]{6}$/)) {
PH_Work=''+PH_IPhone.substring(1);
}
// eleven digits with separators [extension]:
// ex: 1-810-762-3394 or 1-810-762-3394x21
if (PH_IPhone.match(/^1.[2-9][0-9]{2}.[2-9][0-9]{2}.[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(2,5)+PH_IPhone.substring(6,9)+PH_IPhone.substring(10);
}
// eleven digits, two separators [extension]:
// ex: 1-810-7623394 or 1-810-7623394x21
if (PH_IPhone.match(/^1.[2-9][0-9]{2}.[2-9][0-9]{2}[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(2,5)+PH_IPhone.substring(6);
}
// eleven digits, single separator [extension]:
// ex: 1-8107623394 or 1-8107623394x21
if (PH_IPhone.match(/^1.[2-9][0-9]{2}[2-9][0-9]{2}[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(2,5)+PH_IPhone.substring(5);
}
// eleven digits, area code in parens [extension]:
// ex: 1(810)7623394 or 1(810)7623394x21
if (PH_IPhone.match(/^1.[2-9][0-9]{2}.[2-9][0-9]{2}[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(2,5)+PH_IPhone.substring(6);
}
// eleven digits, area code in parens, separator [extension]:
// ex: 1(810)762-3394 or 1(810)762-3394x21
if (PH_IPhone.match(/^1.[2-9][0-9]{2}.[2-9][0-9]{2}.[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(2,5)+PH_IPhone.substring(6,9)+PH_IPhone.substring(10);
}
// plus, 11 digits [extension]:
// ex: +18107623394 or +18107623394x21
if (PH_IPhone.match(/^\+1[2-9][0-9]{2}[2-9][0-9]{6}$/)) {
PH_Work=''+PH_IPhone.substring(2);
}
// plus, 11 digits with separators [extension]:
// ex: +1-810-762-3394 or +1-810-762-3394x21
if (PH_IPhone.match(/^\+1.[2-9][0-9]{2}.[2-9][0-9]{2}.[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(3,6)+PH_IPhone.substring(7,10)+PH_IPhone.substring(11);
}
// plus, 11 digits, two separators [extension]:
// ex: +1-810-7623394 or +1-810-7623394x21
if (PH_IPhone.match(/^\+1.[2-9][0-9]{2}.[2-9][0-9]{6}$/)) {
PH_Work=''+PH_IPhone.substring(3,6)+PH_IPhone.substring(7);
}
// plus, 11 digits, single separator [extension]:
// ex: +1-8107623394 or +1-8107623394x21
if (PH_IPhone.match(/^\+1.[2-9][0-9]{2}[2-9][0-9]{6}$/)) {
PH_Work=''+PH_IPhone.substring(3);
}
// plus, 11 digits, area code in parens [extension]:
// ex: +1(810)7623394 or +1(810)7623394x21
if (PH_IPhone.match(/^\+1.[2-9][0-9]{2}.[2-9][0-9]{6}$/)) {
PH_Work=''+PH_IPhone.substring(3,6)+PH_IPhone.substring(7);
}
// plus, 11 digits, area code in parens, separator [extension]:
// ex: +1(810)762-3394 or +1(810)762-3394x21
if (PH_IPhone.match(/^\+1.[2-9][0-9]{2}.[2-9][0-9]{2}.[0-9]{4}$/)) {
PH_Work=''+PH_IPhone.substring(3,6)+PH_IPhone.substring(7,10)+PH_IPhone.substring(11);
}

if (PH_Work == '') {
alert('Invalid NANP telephone number.');
return PH_IPhone; // output=input
}

// At this point we have ten digits with possible extension, and we can
// process the output formatting.

if ((PH_ix1=PH_FormatO.indexOf('x')) == -1) {
PH_Ext='';
}

PH_Out='';
if ((PH_FormatO == '#######') // seven digits. (default)
¦¦ (PH_FormatO == '#######x?')) { // seven digits plus extension.
PH_Out=''+PH_Work.substring(3,10);
}
if ((PH_FormatO == '###-####') // seven digits with separator.
¦¦ (PH_FormatO == '###-####x?')) { // seven digits, separator, extension.
PH_Out=''+PH_Work.substring(3,6)+PH_Dash+PH_Work.substring(6,10);
}
if ((PH_FormatO == '##########') // ten digits.
¦¦ (PH_FormatO == '##########x?')) { // ten digits plus extension.
PH_Out=''+PH_Work.substring(0,10);
}
if ((PH_FormatO == '###-###-####') // ten digits with separators.
¦¦ (PH_FormatO == '###-###-####x?')) { // ten digits, separators, extension.
PH_Out=''+PH_Work.substring(0,3)+PH_Dash+PH_Work.substring(3,6)+PH_Dash+PH_Work.substring(6,10);
}
if ((PH_FormatO == '###-#######') // ten digits, single separator.
¦¦ (PH_FormatO == '###-#######x?')) { // ten digits, single separator, extension.
PH_Out=''+PH_Work.substring(0,3)+PH_Dash+PH_Work.substring(3,10);
}
if ((PH_FormatO == '(###)#######') // ten digits, area code in parens.
¦¦ (PH_FormatO == '(###)#######x?')) { // ten digits, area code in parens, extension.
PH_Out=''+'('+PH_Work.substring(0,3)+')'+PH_Work.substring(3,10);
}
if ((PH_FormatO == '(###)###-####') // ten digits, area code in parens, separator.
¦¦ (PH_FormatO == '(###)###-####x?')) { // ten digits, a/c in parens, separator, extension.
PH_Out='('+PH_Work.substring(0,3)+')'+PH_Work.substring(3,6)+PH_Dash+PH_Work.substring(6,10);
}
if ((PH_FormatO == '(###) ###-####') // ten digits, a/c parens, space, separator.
¦¦ (PH_FormatO == '(###) ###-####x?')) { // ten digits, a/c parens, space, separator, extension.
PH_Out='('+PH_Work.substring(0,3)+') '+PH_Work.substring(3,6)+PH_Dash+PH_Work.substring(6,10);
}
if ((PH_FormatO == '1##########') // eleven digits.
¦¦ (PH_FormatO == '1##########x?')) { // eleven digits plus extension.
PH_Out=''+'1'+PH_Work.substring(0,10);
}
if ((PH_FormatO == '1-###-###-####') // eleven digits with separators.
¦¦ (PH_FormatO == '1-###-###-####x?')) { // eleven digits, separators, extension.
PH_Out=''+'1-'+PH_Work.substring(0,3)+PH_Dash+PH_Work.substring(3,6)+PH_Dash+PH_Work.substring(6,10);
}
if ((PH_FormatO == '1-###-#######') // eleven digits, two separators.
¦¦ (PH_FormatO == '1-###-#######x?')) { // eleven digits, two separators, extension.
PH_Out=''+'1-'+PH_Work.substring(0,3)+PH_Dash+PH_Work.substring(3,10);
}
if ((PH_FormatO == '1-##########') // eleven digits, single separator.
¦¦ (PH_FormatO == '1-##########x?')) { // eleven digits, single separator, extension.
PH_Out=''+'1-'+PH_Work.substring(0,10);
}
if ((PH_FormatO == '1(###)#######') // eleven digits, area code in parens.
¦¦ (PH_FormatO == '1(###)#######x?')) { // eleven digits, a/c in parens, extension.
PH_Out=''+'1('+PH_Work.substring(0,3)+')'+PH_Work.substring(3,10);
}
if ((PH_FormatO == '1(###)###-####') // eleven digits, area code in parens, separator.
¦¦ (PH_FormatO == '1(###)###-####x?')) { // eleven digits, a/c in parens, separator, ext.
PH_Out=''+'1('+PH_Work.substring(0,3)+')'+PH_Work.substring(3,6)+PH_Dash+PH_Work.substring(6,10);
}
if ((PH_FormatO == '+1##########') // plus, 11 digits.
¦¦ (PH_FormatO == '+1##########x?')) { // plus, 11 digits plus extension.
PH_Out=''+'+1'+PH_Work.substring(0,10);
}
if ((PH_FormatO == '+1-###-###-####') // plus, 11 digits with separators.
¦¦ (PH_FormatO == '+1-###-###-####x?')) { // plus, 11 digits, separators, extension.
PH_Out=''+'+1-'+PH_Work.substring(0,3)+PH_Dash+PH_Work.substring(3,6)+PH_Dash+PH_Work.substring(6,10);
}
if ((PH_FormatO == '+1-###-#######') // plus, 11 digits, two separators.
¦¦ (PH_FormatO == '+1-###-#######x?')) { // plus, 11 digits, two separators, extension.
PH_Out=''+'+1-'+PH_Work.substring(0,3)+PH_Dash+PH_Work.substring(3,10);
}
if ((PH_FormatO == '+1-##########') // plus, 11 digits, single separator.
¦¦ (PH_FormatO == '+1-##########x?')) { // plus, 11 digits, single separator, extension.
PH_Out=''+'+1-'+PH_Work.substring(0);
}
if ((PH_FormatO == '+1(###)#######') // plus, 11 digits, area code in parens.
¦¦ (PH_FormatO == '+1(###)#######x?')) { // plus, 11 digits, a/c in parens, extension.
PH_Out=''+'+1('+PH_Work.substring(0,3)+')'+PH_Work.substring(3,10);
}
if ((PH_FormatO == '+1(###)###-####') // plus, 11 digits, area code in parens, separator.
¦¦ (PH_FormatO == '+1(###)###-####x?')) { // plus, 11 digits, a/c in parens, separator, ext.
PH_Out=''+'+1('+PH_Work.substring(0,3)+')'+PH_Work.substring(3,6)+PH_Dash+PH_Work.substring(6,10);
}

if (PH_Out == '') {
alert('Invalid format requested: '+PH_FormatO+'.');
PH_IPhone=IPhone; // restore to original
return PH_IPhone; // output=input
}

PH_Out=PH_Out+PH_Ext;
return PH_Out;
}

*** End. I think it worked!

[edited by: jatar_k at 5:51 pm (utc) on Aug. 5, 2005]
[edit reason] removed url and email [/edit]

Nutter

5:49 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



Wow! That's great. I'm going to try it next time I get a chance.