The isNumber
function checks if the input value is a valid number. It returns true
if the value is a valid number, and false
otherwise.
The function can be imported using ES6 syntax from the "multiform-validator" package:
import { isNumber } from 'multiform-validator';
Alternatively, you can import the function using CommonJS syntax with require
(Node.js):
const { isNumber } = require('multiform-validator');
The function takes one parameter:
value
(any) - The value to be checked if it represents a valid number.// Example 1 - Valid numbers
const result1 = isNumber(123); // true
console.log(result1);
// Example 2 - Not a valid number
const result2 = isNumber('abc'); // false
console.log(result2);
The function checks if the input value is not null, undefined, or a boolean type. If any of these conditions are met, the function returns false since these values cannot represent valid numbers. For all other types, the function uses the. parseFloat
function to convert the value to a floating-point number. If the conversion results in a valid number and the value is finite (not infinity or NaN), the function returns true, indicating that the input value is a valid number. Otherwise, it returns false.