How to Validate Number String in JavaScript ?
Last Updated :
09 Apr, 2024
Validating a number string in JavaScript involves ensuring that a given string represents a valid number. This typically includes checking for digits, optional signs (+/-), decimal points, and possibly exponent notation (e.g., “1.23e4”). We will use various methods to validate number strings in JavaScript like the isNaN Function, the Number() Function, and the isFinite Function.
Validating number string using the isNaN Function
In this approach for Validating number string the isNaN Function is used. The isNaN
function returns true
if the given value is NaN (Not-a-Number), and false
otherwise. The function takes a string as input and returns true
if it represents a valid number, as determined by the isNaN
function. It returns false
otherwise.
Example: Validate number string in JavaScript using the isNaN Function.
JavaScript
// Validate number string using isNaN function
function isValidNumberIsNaN(str) {
return !isNaN(str);
}
console.log(isValidNumberIsNaN("123"));
console.log(isValidNumberIsNaN("-1.23"));
console.log(isValidNumberIsNaN("3.14e5"));
console.log(isValidNumberIsNaN("abc"));
console.log(isValidNumberIsNaN("123.45.67"));
Outputtrue
true
true
false
false
Validating number string using the Number() Function
In this approach for Validating number string the Number() Function is used. The Number
function converts the given value to a number. If the value cannot be converted to a valid number, it returns NaN. The r
function first converts the input string to a number using the Number
function. It then checks if the resulting value is NaN using the isNaN
function, and returns false
if it is NaN, indicating that the input string is not a valid number.
Example: Validate number string in JavaScript using the Number() Function.
JavaScript
// Validate number string using Number function
function isValidNumberNumber(str) {
return !isNaN(Number(str));
}
console.log(isValidNumberNumber("123"));
console.log(isValidNumberNumber("-1.23"));
console.log(isValidNumberNumber("3.14e5"));
console.log(isValidNumberNumber("abc"));
console.log(isValidNumberNumber("123.45.67"));
Outputtrue
true
true
false
false
Validating number string using the isFinite Function
In this approach for Validating number string the isFinite Function is used. The isFinite
function returns true
if the given value is a finite number and false
otherwise. The function takes a string as input and returns true
if it represents a valid finite number, as determined by the isFinite
function. It returns false
otherwise
Example: Validate number string in JavaScript using the isFinite Function.
JavaScript
// Validate number string using isFinite function
function isValidNumberIsFinite(str) {
return isFinite(str);
}
console.log(isValidNumberIsFinite("123"));
console.log(isValidNumberIsFinite("-1.23"));
console.log(isValidNumberIsFinite("3.14e5"));
console.log(isValidNumberIsFinite("abc"));
console.log(isValidNumberIsFinite("123.45.67"));
Outputtrue
true
true
false
false
Share your thoughts in the comments
Please Login to comment...