Add hyphens to a string to format it as a 16-digit serial number - Javascript String

Javascript examples for String:substring

Description

Add hyphens to a string to format it as a 16-digit serial 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(){//from w ww .  ja  v  a  2 s . co m
var randnum = "12345678910123456145";
var serial = randnum.substring(0,4) + "-" + randnum.substring(5,9) + "-" + randnum.substring(10,14) + "-" + randnum.substring(15,19);
console.log(serial);
    }

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

Related Tutorials