J
J
JackShcherbakov2018-02-02 11:24:02
JavaScript
JackShcherbakov, 2018-02-02 11:24:02

How to check if a number has been entered in input using javascript?

How to check if a number has been entered in input using javascript?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2018-02-02
@tmshchk

Recently I saw a similar task on learn.javascript.ru that solves your problem.

input.onkeypress = function(e) {
  e = e || event;

  if (e.ctrlKey || e.altKey || e.metaKey) return;

  var chr = getChar(e);

  // с null надо осторожно в неравенствах,
  // т.к. например null >= '0' => true
  // на всякий случай лучше вынести проверку chr == null отдельно
  if (chr == null) return;

  if (chr < '0' || chr > '9') {
    return false;
  }
}

A
Andrey Sanych, 2018-02-02
@mountpoint

All integer and floating point values ​​will pass.

if (!isNaN(YOUR_VALUE)) { 

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question