Explain Codes LogoExplain Codes Logo

How can I check if a string is a valid number?

javascript
prompt-engineering
interview-preparation
functions
Anton ShumikhinbyAnton ShumikhinΒ·Sep 24, 2024
⚑TLDR

To formulate a simple check for a valid number in a string, use the Number.isFinite function with a + unary operator acting as a type converter.

function isValidNumber(str) { return Number.isFinite(+str); } console.log(isValidNumber("123")); // true console.log(isValidNumber("-123.456")); // true console.log(isValidNumber("0x11")); // false, hexadecimal ain't our cup of β˜• console.log(isValidNumber("abc")); // false, secret codes don't count πŸ˜‰

This acts as a Swiss army knife, filtering out everything that's not strictly a decimal number, including hexadecimal strings and non-numeric characters.

Numbers in disguise: Understanding the complexity of validation

While Number.isFinite(+str) unlocks the gate for the most common requirements, there are certain corner cases occurring at the cross-section of JavaScript's quirky type coercion and built-in functions that require our attention.

The tricky Infinity and the subtle empty string

Here's the magic of JavaScript: the string '1e10000' evaluates to Infinity in JavaScript. Infinity is considered a number, but you might want to flag it as invalid in your case.

console.log(Number.isFinite('1e10000')); // false, JavaScript goes, "Bro, that's just too much."

And take an empty string ''. The innocent blank is considered equivalent to 0 - could be an unintended side effect!

console.log(isValidNumber('')); // false

Integers vs. Floats: Decimal point matters

For integer decoding, parseInt() is like the best buddy, helping us pull out integers from strings and ditching unwanted trailing content. But that isn't always desirable when absolute string validation is your goal.

console.log(parseInt('123px')); // 123, it's all about the numbers, ditch the 'px'

For floating-point numbers, parseFloat() keeps the decimal intact but permits trailing characters:

console.log(parseFloat('10.5px')); // 10.5, sorry 'px', you're out!

To tighten our grip, we would opt for:

function isStrictlyNumber(str) { return !isNaN(str) && !isNaN(parseFloat(str)) && str.trim() === parseFloat(str).toString(); }

This acts like a hawk eye, permitting only numeric strings devoid of trailing characters.

Looking sharp with regex

For cases where there is a defined pattern for numeric strings, regular expressions have your back.

function isWholeNumber(str) { return /^-?\d+$/.test(str); } function isDecimalNumber(str) { return /^\d+\.\d+$/.test(str); }

These patterns ensure a string is strictly an integer (including negative integers) or a floating-point number, no more or no less.

Not all numbers speak the same language

When it comes to locale-specific number formats like 1.234,56 used in certain European countries, the validation checks would need to be nuanced. You might need to rely on external libraries like Intl.NumberFormat or enforce a uniform number format throughout your app.

Battle-hardened string validation

Getting to grips with those edge cases and employing techniques that catch all scenarios, be it an empty string disguised as a zero or an innocent number-centric string which is not quite a number.

Defending against spaces and tabs

function isNumericAndTrimmed(str) { return str.trim() !== '' && Number.isFinite(+str.trim()); }

This unassuming function helps to catch strings like ' ' or '\n\t' which would otherwise creep in as zero under the hood of JavaScript’s type coercion.

Embracing scientific notation

function isValidScientificNumber(str) { return /^-?\d+(\.\d+)?(e[+-]?\d+)?$/.test(str); }

This regex pattern helps validate numbers in scientific notation, such as 1e-5, a legitimately neat number!

Juggling between safe integers and BigInt

For very large or small numbers that can be represented in JavaScript but exceed the Number.MAX_SAFE_INTEGER or Number.MIN_SAFE_INTEGER, it's safer to line up BigInt or libraries like big.js that can handle arbitrary-precision arithmetic.

Conversions: Handle with care

function safeStringToNumber(str) { if (typeof str === 'string') { return Number.isFinite(+str); } return false; }

The golden rule: ensure the data type is appropriate before crossing the border of type conversion.