-6

As my question says, how to prevent a user from typing non-numeric characters in the input field? Like e.g when they attempt to type alphabets or special characters, the cursor won't move at all inside the input field?

<input id="1stfield" name="1stfield" value="" readonly/>
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
sasori
  • 5,249
  • 16
  • 86
  • 138

1 Answers1

0

You can make it like following as well

<input type="text" class="numbersOnly" value="" />

for above input field, add following code in javascript..

$('.numbersOnly').keyup(function () { 
    this.value = this.value.replace(/[^0-9\.]/g,'');
});
K D
  • 5,889
  • 1
  • 23
  • 35
  • 1
    Bad solution! This will make the cursor go to unexpected places. Try typing with this event on - nightmare. – oriadam Dec 22 '16 at 13:20