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
3 Answers
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
Comments
Try
var input = 1;
console.log(typeof input === "number");
See typeof
var input1 = 1, input2 = "1";
console.log(typeof input1 === "number", typeof input2 === "number");
Comments
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);
}