JSFiddle - React, Tailwind, and code Playground

by gurvinder372

HTML

<label for="serial">Type something magical</label>
<input type="text" id="serial" name="serial">

JavaScript

$('#serial').keyup(function(){

  $(this).val( $(this).val().trim() );

  var str = $(this).val().trim().split( "-" ).join("");
  var output = "";
  while ( str.length > 3 )
  {
   output += str.substring(0,3) + "-";
   str = str.substring(3);
  }
  output += str;
  
  $(this).val(output);

});