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')" >
Tweet
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')" >
Tweet