Home  »     »  JS function to convert string to US phone number format

JS function to convert string to US phone number format

Tuesday, March 1, 2011

Javascript function to change the string to US telephone number format

Eg: if input string is 0115463234 ,it will be converted to (011)546-3234

function toPhoneFormat( obj ){
val=document.getElementById(obj).value;

arrChars=val.split('');
formatedString='';
for(i=0;i < val.length;i++){
if(i==0)
formatedString+='(';
if(i==3)
formatedString+=') ';
if(i==6)
formatedString+='-';

formatedString+=arrChars[i];
}
document.getElementById(obj).value=formatedString;
}




Sample usage:
Function is called in the onBlur event

<input id="txtpersontel" type="text" name="persontel" maxlength="20" style="width:125px"
onblur="toPhoneFormat('txtpersontel')" >




How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
JS function to convert string to US phone number format.




Share this!




comments powered by Disqus

This Weeks 7 Popular Posts


Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   



Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »