Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 560 Bytes

check-if-a-string-contains-only-digits.mdx

File metadata and controls

31 lines (24 loc) · 560 Bytes
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