Skip to main content

isNumeric

A function that checks whether a given string consists only of digits (0-9).

The sign option allows including the "-" sign and decimal point.


Code

🔗 View source code

Interface

typescript
interface Options {
sign?: boolean;
}

function isNumeric(value: string, options?: Options): boolean

Usage

Basic Usage

typescript
import { isNumeric } from '@modern-kit/utils';

isNumeric("123"); // true
isNumeric("0"); // true

isNumeric("-123"); // false
isNumeric("1a"); // false
isNumeric("1@"); // false
isNumeric("1[]"); // false
isNumeric("12.00"); // false
isNumeric(" "); // false

Using the sign option

typescript
import { isNumeric } from '@modern-kit/utils';

isNumeric('123.45', { sign: true }); // true
isNumeric('-123.45', { sign: true }); // true

isNumeric('12.3a45', { sign: true }); // false
isNumeric('12-345', { sign: true }); // false, (the "-" sign can only be placed at the very beginning of the string)
isNumeric('123.', { sign: true }); // false, (no digits after the decimal point)