keyup Padleft

by Al Unser Albuquerque

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<input class="form-control" id="myID" />

<span id="resultado"></span>

JavaScript

// fires on all blur and keyup events
   $(document).on('blur keyup', 'input', function() {
     $('#myID').addLeadingZeros(11);
   });

   // fires on blur and keyup events for #myID
   $(document).on('blur keyup', '#myID', function() {
     $(this).addLeadingZeros(11);
   });

   $.fn.addLeadingZeros = function(length) {
     for (var el of this) {
       _value = el.value.replace(/^0+/, '');
       length = length - _value.length;
       if (length > 0) {
         while (length--) _value = '0' + _value;
       }
       el.value = _value;
     }
   };