3

I have a code that takes some numbers from a user.I want to ensure that the data given from the user are numbers and not strings.From my knowledge to vb6 I know i can use IsNumeric so I was wondering if there is any similar function to js

stranger4js
  • 269
  • 4
  • 15

3 Answers3

5

use isNaN() and pass a string or number to it. It will return true or false. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

zackify
  • 5,314
  • 2
  • 22
  • 28
1

Try

var input = 1;
console.log(typeof input === "number");

See typeof

var input1 = 1, input2 = "1";
console.log(typeof input1 === "number", typeof input2 === "number");
guest271314
  • 1
  • 15
  • 104
  • 177
0

Per the question previously asked here: Is there any function like IsNumeric in javascript to validate numbers

You can find an elegant function to create within your own library as follows:

function isNumeric(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}
Community
  • 1
  • 1
d3v1lman1337
  • 229
  • 1
  • 10