Skip to content

Instantly share code, notes, and snippets.

@debkanchan
Last active February 27, 2025 10:17
Show Gist options
  • Save debkanchan/1eb07af7d9595256535c5c71ea79d66e to your computer and use it in GitHub Desktop.
Save debkanchan/1eb07af7d9595256535c5c71ea79d66e to your computer and use it in GitHub Desktop.
RegEx for Latitude and Longitude

Regular Expression(RegExp) for Latitude and Longitude

Just Latitude:

^-?([0-8]?[0-9]|90)(\.[0-9]{1,10})$

matches:

  • 56.3847
  • -56.387

unmatches:

  • 3
  • 30
  • 300
  • 91.34456
  • 40.238946234652374238746

Just Longitude or Both (Because lat's range is a subset of long)

^-?([0-9]{1,2}|1[0-7][0-9]|180)(\.[0-9]{1,10})$

matches:

  • 56.3847
  • -56.387
  • 91.34456
  • 127.485784
  • -130.37567

unmatches:

  • 3
  • 30
  • 300
  • 40.238946234652374238746
  • 181.394898

Just Latitude with optional decimal numbers:

^-?([0-8]?[0-9]|90)(\.[0-9]{1,10})?$

matches:

  • 3
  • 30
  • 56.3847
  • -56.387

unmatches:

  • 300
  • 91.34456
  • 40.238946234652374238746

Just Longitude or Both with optional decimal numbers (Because lat's range is a subset of long)

^-?([0-9]{1,2}|1[0-7][0-9]|180)(\.[0-9]{1,10})?$

matches:

  • 3
  • 30
  • 56.3847
  • -56.387
  • 91.34456
  • 127.485784
  • -130.37567

unmatches:

  • 300
  • 40.238946234652374238746
  • 181.394898

Note: This RegEx matches to coordinates upto 10 decimal places accurate. To increase or decrease the accuracy change the 10 to your desired accuracy in the last {1,10}.

@debkanchan
Copy link
Author

@anda98 this could be dependent on how your form is validating the regex. You should check the form docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment