Javascript String slice from reverse to format phone number - Javascript String

Javascript examples for String:slice

Description

Javascript String slice from reverse to format phone number

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//  w w  w.  j ava 2s .  co m
var my = "123456789878";

if(my.length === 11)
    var new_no   = "("+my.slice(0,2)+")-"+my.slice(2,7)+"-"+my.slice(7,11);
else if(my.length === 12)
    var new_no   = "("+my.slice(0,2)+")-"+my.slice(2,8)+"-"+my.slice(8,12);
else
    console.log('Invalid Number');

console.log(new_no);

    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials