How to properly check if a string is a number in JavaScript? IsNumeric() function
Hello everyone! I'm writing in JavaScript and encountered a typical problem — I need to check whether a user-entered value is a number. For example, there is a form where the user enters a product price, and I want to make sure it's a number, not text.\n\nI tried using isNaN(), but it behaves strangely. For example:\n\n
console.log(isNaN('123')); // false — seems like a number\nconsole.log(isNaN('12.3')); // false — also a number\nconsole.log(isNaN('')); // false — empty string is considered a number?!\nconsole.log(isNaN(' ')); // false — spaces are also a number?\nconsole.log(isNaN('abc')); // true — this is clear\nconsole.log(isNaN(true)); // false — true=1, is this a number?!\nconsole.log(isNaN(null)); // false — null=0, also a number?!\n\nI need the IsNumeric() function to return true only No answers yet.
Log in to leave an answer