Manish Makkad Manish Makkad - 5 months ago 31
HTML Question

Restrict Exponents and periods(.) in numeric textbox html

I have a numeric textbox, by default numeric textbox allows Exponents and periods.
How do i restrict that?

One of the method i can think of is using string.replace on input event however it is not working as expected. Below is my code..


<input type='number' class='number'>


$(".number").on('input', function () {
this.value = this.value.replace(/e|E|\./g, ''); // Remove e, E, and period(.)
if (this.value.length > 4) {
this.value = this.value.slice(0, 4);

When i enter string '2e' in textbox, entire input is getting removed if above code is ran. i just wants to remove e, E or period from input string not entire input.

Is there a way to achieve this.



So basically you want to allow just numbers? Then restrict the keys by keycode:

   return e.keyCode>47&&e.keyCode<58;

And since you'd like the max length to be 5, just add max="99999":

<input type='number' class='number' max="99999">