jQuery - Field with maximum 4 digits and complete with 0 left

Asked

Viewed 736 times

2

I need a field where 4 numbers can be entered at most, and if the user enters less than 4 digits, the 4 digits are completed with zeros on the front, and does not accept values such as 0, 00, 000 or 0000.

I made the code below by setting a 4 digit mask with the input mask and making appropriate checks to remove the entered value if it is one of the above values (0, 00, 000 or 0000) or complete with zeros if valid, however I could not identify why it is cleaning the entered value by taking the focus of the field after typing a valid value, instead of completing with zeros to the left or keeping the entered value.

jQuery(function($){
    $("#numero").mask("9999");
    $("#numero").bind("change focusout", function (){
       var num = $(this).val();
       if (num == 0 | num == 00 | num == 000 || num == 0000){
          $(this).val('');
       }
       else{
          while (num.size < 4){
             num = 0 + num;
             $(this).val(num);
          }
       }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script>

Número <input type="text" id="numero" name="numero" maxlength="4"/>

3 answers

4


You have to set the option autoclear: false.

And your if should be: if (num == '0___' || num == '00__' || num == '000_' || num == '0000')

jQuery(function($){
    $("#numero").mask("9999", {
      autoclear: false
    });
    $("#numero").bind("change focusout", function (){
       var num = $(this).val();
       if (num == '0___' || num == '00__' || num == '000_' || num == '0000'){
          $(this).val('');
       }
       else{
          num = num.replace('_','').replace('_','').replace('_','');
          while (num.length < 4){
             num = '0' + num;
             $(this).val(num);
          }
       }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script>

Número <input type="text" id="numero" name="numero" maxlength="4"/>

  • It is, but making only this change is not cleaning the input by inserting only 0, 00, 000 or 0000 and focusing the field, and not completing the field with the zeros on the left when inserting a valid number.

  • Take a look again.

  • Now you are only not completing with zeros on the left by entering a valid value, for example, 21.

  • I changed it one more time

  • Thanks for the help. I had already broken my head enough with this and nothing to work.

1

In the if, removes the remaining zeros, compares only with a zero num === 0

There in Isis, you replaced by this:

// Transforma o valor do input num array, e automáticamente coloca um zero pros valores q não forem imputados
var arr = n.toString(10).replace(/\D/g, '0').split('').map(Number);
// Transforma o array numa string
var newNumber = arr.toString();
// Add o valor no input, removendo as virgulas.
$(this).val(newNumber.replace(/,/g,''));
  • I did the test here making the mentioned changes, but is inserting the zeros to the right of the number inserted, instead of inserting to the left. Also, I would like to keep the current code, only correcting the existing error(s) and understanding where I was wrong in the logic made.

1

Dude I made an example for you.

  • 1 - You do not need to use the Mask for this because you are already setando maxlenght in Html.
  • 2 - Your code only had one small error, on the line of while to catch the size is not size, but, yes lenght.

$("#numero").on("change", function(){
   var num = $(this).val(); 
       
   if (num == 0 | num == 00 | num == 000 || num == 0000){
      $(this).val('');
   }
   else{
      while (num.length < 4){
          num = 0 + num;
          $(this).val(num);
      }
   }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script>

Número <input type="text" id="numero" name="numero" maxlength="4"/>

  • But without the mask the field now accepts characters other than numbers, the field must be filled with numbers only. If you type’d', for example, there with the zeros on the left, it is '000d'.

  • But if it’s just a number you want to put the input of the type number. I see no need to load an entire plugin on the page just for this.

  • Had already done the test with the input of the kind number before, however, if you put the type as number, in some browsers does not work the question of completing with zeros on the left (Firefox, for example).

  • Strange, because I’m using exactly the Firefox and is inserting zeros normally.

  • You are probably testing with the current code (with input of the kind text), ai works, but if put as number does not work in Firefox, at least here in my Firefox 61.0.2 (latest version) does not work.

  • Leandro, I am in client and his firefox is so old that it accepted the same zeros of type number :]. Jewel that managed to solve

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.