As you know, the javascript's parseFloat
function works only until it meets an invalid character, so for example
parseFloat("10.123") = 10.123
parseFloat("12=zzzz") = 12
parseFloat("z12") = NaN
Is there a way or an implementation of parseFloat that would return NaN if the whole string is not a valid float number?
Use this instead:
var num = Number(value);
Then you can do:
if (isNaN(num)) {
// take proper action
}
Maybe try:
var f = parseFloat( someStr );
if( f.toString() != someStr ) {
// string has other stuff besides the number
}
Update: Don't do this, use @dcp's method :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With