title: How to automatically add space between digits as they are being entered into a textbox?
body: <p>We have pin numbers in the following format:</p>
  
  <p>45 674 25 910</p>
  
  <p>Our original requirement was to give the users the ability to enter their pin with or without spaces,</p>
  
  <p>In other words, if the pin is 10 digits without spaces, we would like to accept it as valid input.</p>
  
  <p>Similarly, if the pin is 13 digits (with the three spaces) we would also like to accept the input as valid.</p>
  
  <p>If the digits are less than 10 with or without spaces, or more than 13 with spaces, we would like to throw an error that input is invalid.</p>
  
  <p>The script below satisfied the above requirements:</p>
  
  <pre><code>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      $(function () {
          $("#btnSearch").click(function () {
              var result = true;
              if ($('#pin').val().replace(/ /g, '').length == 10) {
                  result = true;
              }
              else {
                  result = false;
                  alert("Invalid");
                  return false;
              }
              return result;
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>However, management has decided to change the requirement to ask that spaces be automatically added while the user is entering the pin numbers.</p>
  
  <p>In other words, users can enter the pin numbers with spaces or they can enter the pin numbers without spaces but that spaces be automatically added while they are typing the pin numbers.</p>
  
  <p>Any ideas how to modify the script above?</p>
  
  <p>Better yet, is there an example that I can modify to meet our requirements?</p>