Insertion of the letter "e" in type number

Asked

Viewed 1,560 times

3

I would like to know how not to allow the letter to be inserted "and", which probably represents the number of Euler, in the input type="number.

  • 1

    I believe that this "e" here represents the scientific notation, indicating the power of 10. Why not allow it to be indicated in the field?

  • 2

    As @Andersoncarloswoss said, from what I could read, the letter "e" has nothing to do with the number of Uler, but rather with the characteristic of the input type="number" accept numbers with scientific notation, risk increasing or decreasing 1, it also accepts negative numbers, i.e., it accepts the two characters "e" and "-", in addition to the 10 digits of the decimal system

  • 3

    In short, instead of typing 10000, the user can type 1e4, which simplifies for those who know the scientific notation - even more when considering even larger numbers. Now, if you don’t want to keep the notation, just use Number. Honestly, I see no reason to stop something that makes the user’s life easier.

  • 3

    @Andersoncarloswoss depends a lot on the system. I see many ways in which allowing scientific notation would just create a mess. This depends a lot on where this data will be sent and how it will be processed.

3 answers

2

One way to do it would be like this, in your HTML:

<input type="number" onkeydown="return FilterInput(event)" onpaste="handlePaste(event)">

And in your Javascript:

<script>
    function FilterInput(event) {
        var keyCode = ('which' in event) ? event.which : event.keyCode;

        isNotWanted = (keyCode == 69 || keyCode == 101);
        return !isNotWanted;
    };

    function handlePaste (e) {
        var clipboardData, pastedData;

        // Get pasted data via clipboard API
        clipboardData = e.clipboardData || window.clipboardData;
        pastedData = clipboardData.getData('Text').toUpperCase();

        if(pastedData.indexOf('E')>-1) {
            //alert('found an E');
            e.stopPropagation();
            e.preventDefault();
        }
};
</script>

0

You could try this way, where #input would be the id of your input:

In Jquery:

$(function() {
    $("#input").keypress(function(event) {
        if (event.which != 8 && event.which != 0 && (event.which < 48 || event.which > 57)) {
            $(".alert").html("Enter only digits!").show().fadeOut(2000);
            return false;
        }
    });
});

In pure Javascript:

document.querySelector(".your_class").addEventListener("keypress", function (evt) {
    if (evt.which != 8 && evt.which != 0 && evt.which < 48 || evt.which > 57)
    {
        evt.preventDefault();
    }
});
  • Could have included the source in the reply.

  • It could of course, but then I’d put all the places I searched to find it, so there’s a combination of some answers. And @Henrique if you have an exact same answer you can mark the source. Or you could put your answer. Or better to ask each person to add their sources, because all the answers here came from other places right ?

  • In fact I only commented because they are the same answers of this question: How to block +,-,e in input type Number?

  • I understood and fact are the same but I did not find them there. But if that’s the case, we agree that you should put the same comment in virtually every answer here in the O.R..

-1


I found something similar in that reply:

var valorNum = document.getElementById("id_input")

valorNum.addEventListener("keydown", function(e) {
  // prevent: "e"
  if ([69].includes(e.keyCode)) {
    e.preventDefault();
  }
})

One point you need to see is that when you reach the maximum value of the field and the user clicks on the arrow (top or bottom) it transforms the number and displays the "and" (ex: 2.18723978932187987e+35), but this code prevents the letter and special characters typed by the user.

Browser other questions tagged

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