-1

I have an input box need to restrict alphabets in keyup function.

I used ASCII value to prevent them.

$('.numbers').keyup(function(e) {
  if (((e.which > 47) && (e.which <
      58)) || (e.which == 46) || (e.which == 8)) {
    console.log("success");
  } else {
    return false;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input id="height" class="numbers" value="172" type="text" name="height">

<input id="weight" type="text" class="numbers" value="55" name="weight">

I need to Restrict the alphabets in my input box.

ankitkanojia
  • 3,072
  • 4
  • 22
  • 35
Arun
  • 31
  • 5

1 Answers1

1

$('.numbers').keypress(function(e) {
  var x = e.which || e.keycode;
  if ((x >= 48 && x <= 57) || x == 8 ||
    (x >= 35 && x <= 40) || x == 46)
    return true;
  else
    return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" class="numbers" />
ankitkanojia
  • 3,072
  • 4
  • 22
  • 35