-2

I have an input (type text) and I want lock typing letters, only numbers (you know: 0, 1, 2, 3, ...) but I don't know how I can do it using plain JavaScript (it is important, I don't want use jQuery).

My input:

<input type="text" id="input_number" class="input_child" value="" placeholder="1">
Fletcher
  • 15
  • 3
Jakub
  • 115
  • 1
  • 2
  • 12
  • http://stackoverflow.com/questions/3059559/restrict-a-character-to-type-in-a-text-box This post details the solution to the problem you are having. Also see this page about doing it natively in html5 https://www.w3schools.com/tags/att_input_pattern.asp – Jonathan Bartlett Mar 26 '17 at 10:06

1 Answers1

0

To indicate whether an input is a number or not you can use the isNaN() function. isNaN (is Not a Number) returns true if the given value is not a number.

In the following snippet you can see isNaN() in action.

var num = document.getElementById("input_number").value;
console.log(num);

// true if num is not a number
// false if num is a number
if (isNaN(num)) 
  console.log("no number");
else
  console.log("is a number");
<input type="text" id="input_number" class="input_child" value="test" placeholder="1">
C0reTex
  • 112
  • 7