Note Field Mask - 10 or 5.5

Asked

Viewed 1,314 times

3

I’m looking for a script on jQuery to make a mask:

When I’m through with you 10, the field format to 10,0 and when it’s another note like 6,5 leave like this.

As an example below.

inserir a descrição da imagem aqui

I need you to delimite the moment you type and not after you click out of the field.

2 answers

5


look, I’ll post a suggestion, where the mask always has a decimal place and only allows numbers between 0 and 10.

var notas = document.getElementsByClassName("nota"); 

var onNotaInput = function (event) {
  var regexp = new RegExp("[^0-9]", "g");
  var value = event.target.value.replace(regexp, "");
  value = parseInt(value) / 10;
  if (value >= event.target.min && value <= event.target.max) {
    event.target.dataset.value = value;
  } else {
    value = parseFloat(event.target.dataset.value);
  }
  if (isNaN(value)) {
    value = 0;
  }

  event.target.value = value.toLocaleString(undefined, { minimumFractionDigits: 1 });
};

[].forEach.call(notas, function (nota) {
  nota.addEventListener("input", onNotaInput);
});
<label>
  Nota 01:
  <input class="nota" type="text" min="0" max="10" />
</label>
<br />
<label>
  Nota 02:
  <input class="nota" type="text" min="0" max="10" />
</label>
<br />
<label>
  Nota 03:
  <input class="nota" type="text" min="0" max="10" />
</label>
<br />
<label>
  Nota 04:
  <input class="nota" type="text" min="0" max="10" />
</label>

  • Exactly Tobymosque only a doubt with ID caught only one input is not? as it would be with various inputs tried to put "Document.getElementsByClassName" but error "addeventlistener"

  • .getElementsByClassName() returns a collection, so you have scroll through it.

  • could you tell me how you do?

  • @Thallesdaniel, the answer is already updated

  • Really good. If you add the if I used to ignore arrow keys, Backspace etc would look even better.

  • no need, the event input is triggered only when the value of the input changes, so it is not triggered when an arrow is used, but is triggered when a value is pasted into the input.

Show 1 more comment

4

If I understand correctly, you want to format the field with a decimal place. Try this code:

$("#valor").on("change", function()
{
    this.value = Number(this.value.replace(",", ".")).toFixed(1).replace(".", ",");
});

Fiddle

Updating

The closest I could get with Masked input was this:

$.mask.definitions['~']='([0-9] )?';
$("#valor").mask("~9,9", {placeholder: " "});

Fiddle

Based in that reply.

But the mask was a *osta and I could not limit the number to 10.

Update 2

Making a mask in the hand, specific to your case, I arrived at this code:

$("#valor").on("keyup", function(e)
{
    var code = (e.keyCode || e.which);

    // do nothing if it's an arrow key or backspace
    if(code == 37 || code == 38 || code == 39 || code == 40 || code == 8) {
        return;
    }

    var num = Number(this.value.replace(",", "."));

    if (this.value.replace(",", "").length > 2) num = num * 100;

    var value = (num <= 10 ? num : 10);

    this.value = value.toFixed(1).replace(".", ",");
});

Fiddle

  • i need it to be like Masked Input at the time of typing and not after clicking off :(

  • 1

    I think it makes no sense what he wants. If I type in the field 55, he wants you to show 5,5. But if I type 10 to leave 10. But with the mask 1,0. The right thing in this case, for me, is just to treat the .(point) and let , (comma) only. And the user who type right! Just check if the note is above 10 or below 10.

  • 1

    @Thallesdaniel then has a limit ? From 0 to 10 ? To format in real time, just change the event to keyup, example, but there the field needs other treatments.

  • I need when I type 10 to be "10.0" and when I type 5.5 to be "5.5" in Masked Input it already leaves limited as you write but it is 10.0 and when minor digit is 05.0. Just wanted to change to qnd for 10 or less would be notes understood? Student’s grade 10.0 or 5.6 example

  • I still don’t understand. Now I think you’re asking the opposite of what you asked for in the beginning. I think you should edit your POST and write exactly what you need, with examples... Narrate what you want. Type: Example 1 - I want to type in the field 10,0 and I want you to show up 10. Example 2 - but when I type 5,5 I want it to stay that way. At least that’s what I initially understood..

  • @Diegosouza is a field of student grade input. I need you to delimite at the moment that the person type if she type 10 the field is 10.0 and if she type below that example 8 would be just like that 8.0 . And if she type 5,7 a real value would look like this msm 5,7 understood but I need to do this at the time that type as the maskedinput does

  • @Thallesdaniel update.

  • @Thallesdaniel one more update.

  • With 10 it works, but with grade 8.5 it won’t.

  • @Thallesdaniel I managed to type 8,5. It is not the perfect mask, but it breaks a branch. Better than that only a more complex code.

  • @Thallesdaniel but the mask I posted from Masked input seems to suit the format of your print.

Show 6 more comments

Browser other questions tagged

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