Latitude/Longitude Coordinate Pair Regex
Validates a comma-separated 'latitude,longitude' coordinate pair, enforcing the real-world ranges of -90 to 90 for latitude and -180 to 180 for longitude.
Regex Pattern
^-?(90(\.0+)?|[1-8]?[0-9](\.[0-9]+)?),\s*-?(180(\.0+)?|1[0-7][0-9](\.[0-9]+)?|[1-9]?[0-9](\.[0-9]+)?)$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string |
| -?(90(\.0+)?|[1-8]?[0-9](\.[0-9]+)?) | A valid latitude value from -90 to 90, with optional decimal precision |
| ,\s* | A literal comma separating latitude and longitude, followed by zero or more whitespace characters |
| -?(180(\.0+)?|1[0-7][0-9](\.[0-9]+)?|[1-9]?[0-9](\.[0-9]+)?) | A valid longitude value from -180 to 180, with optional decimal precision |
| $ | Anchors the match to the end of the string |
Detailed Explanation
What it does
This pattern validates a single string containing both a latitude and a longitude, separated by a comma and optional whitespace, such as '40.7128,-74.0060' or '40.7128, -74.0060'. Each component is validated against its own real-world range: latitude within -90 to 90, longitude within -180 to 180.
Why it works
The pattern reuses the same range-limited group structure as the standalone latitude and longitude patterns, joined by a literal comma and \s* to tolerate an optional space after the comma (a common formatting convention). Because each half is independently anchored to its own valid numeric range using the same boundary-safe alternations, a swapped or out-of-range value in either half causes the whole match to fail, catching common data-entry mistakes like an accidentally swapped lat/lng pair.
Common use cases
- Validating a single 'lat,lng' text field in a location-picker form
- Parsing coordinate strings from CSV exports, map share links, or clipboard pastes
- Sanity-checking geocoded data before storing it in a database column
- Detecting swapped latitude/longitude values, since a swapped pair with a latitude value over 90 in the longitude-shaped 'latitude' slot will fail to match
Edge cases
- A space after the comma, as in '40.7128, -74.0060', is accepted because of the \s* between the components
- No space at all, as in '40.7128,-74.0060', is equally valid since \s* also matches zero whitespace characters
- Boundary pairs like '90,180' and '-90,-180' are valid since both components sit exactly on their respective limits
- A pair with an out-of-range value in either slot, such as '91,0' or '40.7128,-181', is correctly rejected
Limitations
- Assumes a comma-separated 'latitude,longitude' order; does not detect a swapped pair that still happens to fall within both valid ranges (e.g. '45,45')
- Does not accept alternative separators such as a semicolon or a space-only delimiter; extend the pattern if those formats are also expected
- Does not validate degrees-minutes-seconds (DMS) notation or coordinate strings wrapped in parentheses or brackets
- Whitespace is only tolerated after the comma, not before it; a string like '40.7128 , -74.0060' with a space before the comma will not match
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const latLngRegex = /^-?(90(\.0+)?|[1-8]?[0-9](\.[0-9]+)?),\s*-?(180(\.0+)?|1[0-7][0-9](\.[0-9]+)?|[1-9]?[0-9](\.[0-9]+)?)$/;
console.log(latLngRegex.test('40.7128,-74.0060')); // trueCommon Mistakes
Validating latitude and longitude with a single generic numeric pattern shared between both, which allows an out-of-range value like '95,200' to pass
Fix: Give each half its own range-limited alternation matching the real-world bounds (-90/90 for latitude, -180/180 for longitude)
Splitting the string on a comma in application code and validating each half separately without also checking the overall format (extra commas, missing separator)
Fix: Use a single anchored regex like this one to validate the whole 'lat,lng' string in one pass
Not accounting for an optional space after the comma, rejecting common human-typed formats like '40.7128, -74.0060'
Fix: Include \s* after the comma to tolerate zero or more spaces without being overly permissive elsewhere
Performance Notes
- Concatenating two small, non-overlapping alternations with a literal separator keeps the overall pattern linear-time with negligible backtracking
- Anchoring with ^ and $ lets the engine reject malformed pairs (wrong separator, extra text) as soon as a mismatch is found
- For validating large batches of coordinate strings, precompile the regex once and consider splitting on the comma first if you also need the individual numeric values for further processing
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |