html – Inserting the letter "e" in type number

Question:

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

Answer:

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>
Scroll to Top