validate number field in javascript can not take negative value code example

Example 1: no negative prices in field type number

<input type="number" min="0">

Example 2: check if number is negative javascript

const positive = 5;
const negative = -5;
const zero = 0;

Math.sign(positive); // 1
Math.sign(negative); // -1
Math.sign(zero); // 0

Example 3: check if number is negative javascript

Math.sign() has 5 possible return values:
1 // positive number
-1 // negative number
0 // positive zero
-0 // negative zero
NaN // not a number

Tags:

Misc Example