title: jQuery Dynamic Masking
body: <p>I have used jQuery Masking plugin in my App.</p>
  
  <p>Link is given here.</p>
  
  <p><a href="https://github.com/digitalBush/jquery.maskedinput" rel="nofollow noreferrer">https://github.com/digitalBush/jquery.maskedinput</a></p>
  
  <p>I need the dynamic masking like.</p>
  
  <p>Sometime I will need to use like this
  000-00-0
  0000000-00-0</p>
  
  <p>Suppose I write 50000 then it would become 50-00-0</p>
  
  <p>Suppose I write 500000 then it would become 500-00-0 </p>
  
  <p>So my first section will contain 1 to 7 different numbers.</p>
  
  <p>So how i can do this.</p>
  
  <p>Simple masking i am using like in below.</p>
  
  <pre><code>$("input[name='mask']").mask("9999999-99-9");
  </code></pre>
  
  <p><strong>Solution</strong></p>
  
  <pre><code>var my_condition = true;
      $("input[name='mask']").on('click focus', function(){ 
         $("input[name='mask']").unmask();
         my_condition = true;
      });  
      $("input[name='mask']").on('change paste', function() {
          var len = $(this).val().length;
              if(my_condition) {
               switch(len) {
                   case 5:
                       $("input[name='mask']").mask('99-99-9');
                       break;
                   case 6:
                       $("input[name='mask']").mask('999-99-9');
                       break;
                   case 7:
                       $("input[name='mask']").mask('9999-99-9');
                       break;
                   case 8:
                       $("input[name='mask']").mask('99999-99-9');
                       break;
                   case 9:
                       $("input[name='mask']").mask('999999-99-9');
                       break;
                   case 10:
                       $("input[name='mask']").mask('9999999-99-9');
                       break;
                   default:
                        $("input[name='mask']").mask('9999');   
               }
               my_condition = false;
           }
      });
  </code></pre>
  
  <p>Thanks</p>