Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Telephone Number Formatting

Status
Not open for further replies.

Mushead

Programmer
May 8, 2003
3
US
How do i validate and format and telephone number in a text box to look like this: (###)###-####.
using java
 
you should be able to find a regular expression pattern to match that with a search on google.com.

one I like

____________________________________________________
get the best answer to your questions by asking the best questions "General FAQ" faq333-2924
onpnt2.gif
 
This won't help you
faq216-3343
because it's in JavaScript, but that's the forum in which you asked, so that's the answer I'll offer ya!

Cheers,


[monkey] Edward [monkey]

"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
did I sense a bit of <sarcasim> in that response. [lol]

____________________________________________________
get the best answer to your questions by asking the best questions &quot;General FAQ&quot; faq333-2924
onpnt2.gif
 
Darn allergies bring my inner li'l stinker to the surface... [lol]

Of course, it's my problem for hiding the thing in the deceptively named &quot;FAQ&quot; section.

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
I'm just a die-hard RegEx user so I usually point to those references. I'll remember the FAQ though from now on. [wink]

____________________________________________________
get the best answer to your questions by asking the best questions &quot;General FAQ&quot; faq333-2924
onpnt2.gif
 
Well dude, if there's a nifty RegEx method, then by all means post it into the FAQ! [smile]

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Probably this will help. I know the code is long for just the formatting however I didn't want to sacrifice the understanding of the idea for a more compact code.
If you need to modify and make it more suitable for your project please do.
The Alert() is thre only for debugging purposes you can either comment that out or just delete it.


function formatphone(digits,format){
var txtphone;

var txtfirst=digits.substr(0,3);
var txtmiddle=digits.substr(3,3);
var txtlast=digits.substr(6,4);

switch (format){
case 0:
//have a format here
break;
case 1:
//have another format here
break;
case 2:
txtphone=&quot;(&quot;+txtfirst+&quot;)&quot;+txtmiddle+&quot;-&quot;+txtlast;
break;

}//endswitch

return txtphone;
}

function checkphone(elem){
var phoneFormats=new Array();
phoneFormats[0]=/^\d{10}$/; //1234567890
phoneFormats[1]=/^\(\d{3}\)\d{3}\-\d{4}$/; //(123)456-7890
phoneFormats[2]=/^\d{3}\-\d{3}\-\d{4}$/; //123-456-7890
phoneFormats[3]=/^\d{3}\.\d{3}\.\d{4}$/; //123.456.7890
// you can add as many different formats and combinations as you want by adding elements to the array.
var boophone=false;
for(var i=0;i<phoneFormats.length;i++){

alert(elem.value+' : '+phoneFormats.test(elem.value)+':'+ phoneFormats)
if(phoneFormats.test(elem.value)){
boophone=true;
}//endif
}//endfor

if(!boophone){
alert('The phone number is incorrect');
elem.select();
elem.focus();
}else{
//do any kind of formatting here. even call a function that does the formatting for you.
elem.value=formatphone(elem.value.replace(/[^\d]/g,''),2);//pass only the digits and from there let the function format the phone based in an index of your preference
}

}//endfunc
</script>

<body >
<input type=&quot;text&quot; name=&quot;telephone&quot; onblur=&quot;checkphone(this)&quot;>
</body>

grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
nice post!
about the only thing I would do is imbed a (\s) replace for spaces in the patterns. Most people in the US are going to format it (222) 333-4444 note the space between the ) 3. this will fail the tests on /^\(\d{3}\)\d{3}\-\d{4}$/;

something like this
function checkphone(elem){
alert(elem.replace(/\s/gi,&quot;&quot;));
var phoneCheck = /^\(\d{3}\)\d{3}\-\d{4}$/.test(elem.replace(&quot;\s&quot;,&quot;&quot;));
}

if(!checkphone(&quot;\(222\) 333-4444&quot;)) {
alert(&quot;Phone format OK!&quot;);
} else {
alert(&quot;Phone format NOT OK!&quot;);
}

____________________________________________________
get the best answer to your questions by asking the best questions &quot;General FAQ&quot; faq333-2924
onpnt2.gif
 
um.. looks like my quick type has some backwards logic [smile]

____________________________________________________
get the best answer to your questions by asking the best questions &quot;General FAQ&quot; faq333-2924
onpnt2.gif
 
correction to the pattern for repalce of spaces
elem.replace(/\s/g,&quot;&quot;)

____________________________________________________
get the best answer to your questions by asking the best questions &quot;General FAQ&quot; faq333-2924
onpnt2.gif
 
you are correct.
Not only I forgot to do that but also a general replace of white spaces
for example
123 456 7890
123. 456. 7890

It's my personal belief that all of them should pass the test and be considered as valid phone number.

As usual I got caught in the formatting for tek-tips and the name of the index &quot;i&quot;. please replace the previous code I posted with this new one.
again thos bolded alerts are for debugging only.

<script type=&quot;text/javascript&quot;>

function formatphone(digits,format){
var txtphone;

var txtfirst=digits.substr(0,3);
var txtmiddle=digits.substr(3,3);
var txtlast=digits.substr(6,4);

switch (format){
case 0:
//have a format here
break;
case 1:
//have another format here
break;
case 2:
txtphone=&quot;(&quot;+txtfirst+&quot;)&quot;+txtmiddle+&quot;-&quot;+txtlast;
break;

}//endswitch

return txtphone;
}

function checkphone(elem){
var phoneFormats=new Array();
phoneFormats[0]=/^\d{10}$/; //1234567890
phoneFormats[1]=/^\(\d{3}\)\d{3}\-\d{4}$/; //(123)456-7890
phoneFormats[2]=/^\d{3}\-\d{3}\-\d{4}$/; //123-456-7890
phoneFormats[3]=/^\d{3}\.\d{3}\.\d{4}$/; //123.456.7890
// you can add as many different formats and combinations as you want by adding elements to the array.
var boophone=false;
var txtphone=elem.value.replace(/\s/g,'');
alert('phone: |'+txtphone+'|');
for(var index=0;index<phoneFormats.length;index++){

alert(elem.value+' : '+phoneFormats[index].test(txtphone)+':'+ phoneFormats[index])
if(phoneFormats[index].test(txtphone)){
boophone=true;
}//endif
}//endfor

if(!boophone){
alert('The phone number is incorrect');
elem.select();
elem.focus();
}else{
//do any kind of formatting here. even call a function that does the formatting for you.
elem.value=formatphone(elem.value.replace(/[^\d]/g,''),2);//pass only the digits and from there let the function format the phone based in an index of your preference
}

}//endfunc
</script>




grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top