I've got an app that users input coordinates into.
In the DB and most mapping software they use the decimal notation for lat/lng (eg. 123.1234) rather than the older format: 34N 40' 50.12"
I need to test that a value input into a form is a float, and not a string. But using parseFloat on 34N 40' 50.12" returns 34-- which validates using most tests.
Here's what I'm trying, which is a mashup of a few really clever solutions found here-- but so far I can't get the thing to work properly for all cases. The basic cases I'm testing for are:
123.1234-- valid'123.1234'-- valid34N 40' 50.12"-- invalid'34N 40' 50.12"'-- invalid123--valid'123'-- valid
Here's a jsfiddle of what I've been trying: http://jsfiddle.net/zfwAj/
3 Answers 3
Seems I should have posted as an answer
isNaN() should work to filter out those ones jsfiddle.net/QYMRe
2 Comments
I suggest this one:
/^-?(\d*\.\d+|\d+(\.\d+)?)$/.test(str)
This accepts negative numbers, and float like (.42)
Fiddle test
isNaNshould work to filter out those ones: jsfiddle.net/QYMRe