How can I check if a string is a float?
Solution 1:
Like this:
if (!isNaN(value) && value.toString().indexOf('.') != -1)
{
alert('this is a numeric value and I\'m sure it is a float.');
}
Solution 2:
You can use the parseFloat
function.
If the value passed begins with what looks like a float, the function returns the value converted to a float, otherwise it will return NaN.
Something like:
function beginsWithFloat(val) {
val = parseFloat(val);
return ! isNaN(val);
}
console.log(beginsWithFloat("blabla")); // shows false
console.log(beginsWithFloat("123blabla")); // shows true