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 Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

Insert dash after every 2nd character in input Insert dash after every 2nd character in input Reviewed by Anil Singh on 10:17 PM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^