How can I check if a string is a valid number?
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.
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.
And take an empty string ''
. The innocent blank is considered equivalent to 0
- could be an unintended side effect!
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.
For floating-point numbers, parseFloat()
keeps the decimal intact but permits trailing characters:
To tighten our grip, we would opt for:
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.
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
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
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
The golden rule: ensure the data type is appropriate before crossing the border of type conversion.
Was this article helpful?