[Solved-5 Solutions] Is there a (built-in) way in javascript to check if a string is a valid number ? - javascript tutorial
Problem:
Is there a built-in way in javascript to check if a string is a valid number ?
Solution 1:
To check if a variable (including a string) is a number, check if it is not a number:
This works regardless of whether the variable contains is a string or number.
isNaN(num) // returns true if the variable does NOT contain a valid number
For Example:
isNaN(123) // false
isNaN('234') // false
isNaN('1f10000') // false (number is Infinity)
isNaN('head') // true
isNaN('20px') // true
To implement the IsNumeric
:
function isNumeric(num){
return !isNaN(num)
}
To convert a numeric value of the sting: It only works if the string contains numeric characters, else it returns NaN.
+num // returns the numeric value of the string, or NaN if the
// string isn't purely numeric characters
Example:
+'11' // 11
+'11.' // 11
+'11..' // Nan
+'.11' // 0.11
+'..11' // Nan
+'too' // NaN
+'11px' // NaN
To convert a string loosely to a number which is useful for converting '11px' to 11, for example.
parseInt(num) // extracts a numeric value from the
// start of the string, or NaN.
Example:
parseInt('11', 10) // 11
parseInt('aaa', 10) // NaN
parseInt('11px', 10) // 11
parseInt('foo1', 10) // NaN These last two may be different
parseInt('11a5', 10) // 11 from what we expected to see.
Floats: Keep in mind that, unlike +num, parseInt (as the name suggests) will convert a float into an integer by cutting off everything following the decimal point (if we want to use parseInt() because of this behaviour, you're possibly better off with Math.floor() instead):
parseInt(11.345, 10) // 11
parseInt('11.345', 10) // 11
+'11.345' // 11.345
Empty strings may be a little counter-intuitive. +num converts empty strings to zero, and isNaN()assumes the same:
+'' // 0
isNaN('') // false
But parseInt() does not agree:
parseInt('', 10) // NaN
Read Also
Python String Method.Solution 2:
Using RegExp:
var num = "978726";
if(num.match(/^\d+$/)){
//valid integer
}else if(num.match(/^\d+\.\d+$/)){
//valid float
}else{
//not valid number
}
Solution 3:
To check if a string is a whole number (no decimal places).
function isNumeric(value) {
return /^\d+$/.test(value);
}
console.log(isNumeric('asdf')); // false
console.log(isNumeric('345a')); // false
console.log(isNumeric('23')); // true
console.log(isNumeric('0987654321')); // true
console.log(isNumeric(2345)); // true
console.log(isNumeric('345.4')); // false
console.log(isNumeric('')); // false
console.log(isNumeric(undesigned)); // false
console.log(isNumeric(null)); // false
Allows positive whole numbers.
function isNumeric(value) {
return /^\d+$/.test(value);
}
console.log(isNumeric('123')); // true
console.log(isNumeric('-23')); // false
Read Also
Python String Format.Solution 4:
Scientific Notation:
Scientific notation
!isNaN('1e+30')
is true.
Large floating numbers:
Using Node.js:
> var s = Array(17 + 1).join('8')
undefined
> s.length
17
> s
'88888888888888888'
> !isNaN(s)
true
> Number(s)
100000000000000000
> String(Number(s)) === s
false
>
On the other side:
> var s = Array(17 + 1).join('1')
undefined
> String(Number(s)) === s
true
> var s = Array(16 + 1).join('8')
undefined
> String(Number(s)) === s
true
>
String(Number(s)) === s, limit the strings to 16 digits at most (after omitting leading zeros).
Infinity
> typeof Infinity
'number'
> !isNaN('Infinity')
true
> isFinite('Infinity')
false
>
To check the given string is a number satisfying all of the following:
- Non scientific notation
- Predictable conversion to Number and back to String
- Finite
It is a simple task.
function isNonScientificNumberString(o) {
if (!o || typeof o !== 'string') {
// Should not be given anything but strings.
return false;
}
return o.length <= 15 && o.indexOf('e+') < 0 && o.indexOf('E+') < 0 && !isNaN(o) && isFinite(o);
}
Solution 5:
Use this code:
function checkNumber(value) {
if ( value % 1 == 0 )
return true;
else
return false;
}