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
Asked
Active
Viewed 6,054 times
3
-
http://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric – juvian Dec 04 '14 at 19:06
-
isFinite() is the closest match – dandavis Dec 04 '14 at 19:16
3 Answers
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