Number Validation in JavaScript (original) (raw)
Last Updated : 03 Jan, 2025
Here are the various methods to validate numbers in JavaScript
1. Check if a Value is a Number
Use typeof and isNaN() to determine if a value is a valid number.
JavaScript `
const isNum = n => typeof n === 'number' && !isNaN(n);
console.log(isNum(42)); console.log(isNum('42')); console.log(isNum(NaN));
`
**In this example
- typeof n === 'number' checks if the type of n is a number.
- !isNaN(n) ensures the value is not NaN (Not a Number).
- It ensures both type and value validity.
2. Check if a String is Numeric
Use regular expressions or isFinite() to validate numeric strings.
JavaScript `
const isNumStr = s => !isNaN(s) && isFinite(s);
console.log(isNumStr('123')); console.log(isNumStr('123abc')); console.log(isNumStr('1.23'));
`
**In this example
- !isNaN(s) ensures the string can be parsed as a number.
- isFinite(s) ensures the value is not infinite.
- This works for integers and decimals.
3. Validate Integer
Check if a number is an integer using Number.isInteger().
JavaScript `
const isInt = n => Number.isInteger(n);
console.log(isInt(42)); console.log(isInt(3.14)); console.log(isInt('42'));
`
**In this example
- Number.isInteger(n) directly validates if n is an integer.
- It doesn't convert strings or other types to numbers.
- The function is simple and precise.
4. Validate Floating-Point Numbers
Use a combination of checks to ensure a number is a float.
JavaScript `
const isFloat = n => typeof n === 'number' && !Number.isInteger(n);
console.log(isFloat(3.14)); console.log(isFloat(42)); console.log(isFloat('3.14'));
`
**In this example
- typeof n === 'number' ensures the value is a number.
- !Number.isInteger(n) confirms it's not an integer.
- This validates numbers with decimal points.
5. Range Validation
Ensure a number falls within a specific range.
JavaScript `
const inRange = (n, min, max) => n >= min && n <= max;
console.log(inRange(10, 5, 15)); console.log(inRange(20, 5, 15)); console.log(inRange(5, 5, 15));
`
**In this example
- The function checks if n is greater than or equal to min and less than or equal to max.
- It’s a quick way to validate ranges.
6. Regex for Strict Validation
Use regular expressions for specific number formats.
JavaScript `
const isStrict = s => /^-?\d+(.\d+)?$/.test(s);
console.log(isStrict('123')); console.log(isStrict('-123.45')); console.log(isStrict('abc123'));
`
**In this example
- The regex ^-?\d+(\.\d+)?$ checks for optional negative signs, digits, and decimals.
- This ensures strict numeric validation for strings.
7. Validate Positive Numbers
Check if a number is positive.
JavaScript `
const isPos = n => typeof n === 'number' && n > 0;
console.log(isPos(42)); console.log(isPos(-42)); console.log(isPos(0));
`
**In this example
- typeof n === 'number' ensures the input is a number.
- n > 0 validates that the number is positive.
- It excludes zero and negative numbers.
8. Validate Negative Numbers
Check if a number is negative.
JavaScript `
const isNeg = n => typeof n === 'number' && n < 0;
console.log(isNeg(-42)); console.log(isNeg(42)); console.log(isNeg(0));
`
**In this example
- typeof n === 'number' ensures the input is a number.
- n < 0 checks if the number is negative.
- It excludes zero and positive numbers.
9. Validate Even Numbers
Determine if a number is even.
JavaScript `
const isEven = n => typeof n === 'number' && n % 2 === 0;
console.log(isEven(4)); console.log(isEven(7)); console.log(isEven(-2));
`
**In this example
- typeof n === 'number' ensures the input is a number.
- n % 2 === 0 checks if the remainder is zero when divided by 2.
- It works for both positive and negative numbers.
10. Validate Odd Numbers
Check if a number is odd.
JavaScript `
const isOdd = n => typeof n === 'number' && n % 2 !== 0;
console.log(isOdd(7)); console.log(isOdd(4)); console.log(isOdd(-3));
`
**In this example
- typeof n === 'number' ensures the input is a number.
- n % 2 !== 0 checks if the remainder is not zero when divided by 2.
- It works for both positive and negative numbers.