category | created | title | updated |
---|---|---|---|
Validator |
2020-05-09 |
Check if a string contains only digits |
2021-10-13 |
JavaScript version
const isNumeric = (str) => !/[^0-9]/.test(str);
TypeScript version
const isNumeric = (str: string): boolean => !/[^0-9]/.test(str);
Examples
isNumeric(2); // true
isNumeric('23'); // true
isNumeric('00123'); // true
isNumeric('1.23'); // false
isNumeric('-Infinity'); // false
isNumeric('Infinity'); // false
isNumeric('NaN'); // false