Insert dash after every 2nd character in input

Insert dash after every 2nd character in input

Insert dash after every 2nd character in input using JavaScript -

Example looks like -
<script>
  $('.shortCodeText').keyup(function() {
    var shortCode = $(this).val().split("-").join(""); // remove hyphens

    if (shortCode.length > 0) {
      shortCode = shortCode.match(new RegExp('.{1,2}', 'g')).join("-");
    }

    $(this).val(shortCode);
   
  });

  $('#btnclick').on('click', function(){
      console.log($('.shortCodeText').val());
  });
</script>

And HTML
<div>
    <input type="text" class="shortCodeText" max="6" maxlengthh="6" />
    <a id="btnclick">Result</a>
</div>

And the live Result - http://jsfiddle.net/26hvo5ed/

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^