chris
chris

Reputation: 36937

Regex to match Longitude and Latitude

I am trying to validate inputs that can be altered by users in one shape way form or another. That are specific to longitude and latitude. So I am trying to come up with a regex that will validate the entry. So I can throw an exception if it doesnt match

The regex I am playing with via php preg_match is:

preg_match('/^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$/')

which doesn't seem to work for me. I am trying to catch gps long/lat in the format like -101.3335 and 71.5555 (of course I dunno how valid those specific numbers are but thats the general format)

Upvotes: 1

Views: 1823

Answers (2)

chris
chris

Reputation: 36937

preg_match('/^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$/')

Is actually a working concept. When I posted, and was having trouble with it I was initally failing to realize I couldn't put one or the other as far as long/lat goes in. I would have to put them both in delimited by a , to get it to validate. Which someone pointed out in my comments to the inital post, I appreciate the help though.

Upvotes: 3

Dan
Dan

Reputation: 736

Try this:

preg_match('/^(\\-?\\d+[.\\d]+),?\\s*(\\-?\\d+[.\\d]+)?$/')

Upvotes: 1

Related Questions