Format Phone Number

[Total: 0    Average: 0/5]

Format Phone numbers with javascript.

Head Code:

<Script>
var _OUTPUT=1; /* set to 1 to have the function output the result -return true if converted
                * set to 0 to have the function not output anything -returns the value
				*/
function formatPhone(num)
{ 
  var _return=false;
  /*
   * 7181238748 to 1(718)123-8748
   */ 

  if(num.length != 10)
  { 
    /* 
     * if user did not enter 10 digit phone number then simply print whatever user entered 
     */ 
	_return=_OUTPUT?num:false;
  } 
  else
  { 
    /* formating phone number here */ 
	_return="1(";
	var ini = num.substring(0,3);
	_return+=ini+")";
	var st = num.substring(3,6);
	_return+=st+"-";
	var end = num.substring(6,10);
	_return+=end;
  }
  return _return; 
} 


</Script> 

Body Code:

<Script>
var _A=new Array(7188746587,"7188746587",1234567,1234567890);
for(var _i=0;_i<_A.length;_i++)
  document.write("<HR>Format "+_A[_i]+":<BR><DIR>"+formatPhone(_A[_i])+"</DIR><BR>");
</Script> 

Times Viewed: 2

Leave a Reply

Your email address will not be published. Required fields are marked *