Skip to content

Instantly share code, notes, and snippets.

@debkanchan
Last active May 9, 2024 15:12
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

Tip: If anyone who is looking at this is thinking of implementing it in Firebase Realtime Database Security Rules, convert lat/long to string first but appending +"" to it like this: (newData.child("Latitude").val()+"").matches(/^-?([0-8]?[0-9]|90)(\.[0-9]{1,10})$/)

@cranesandcaff
Copy link

I did see 300 pass this when doing my own testing of it

@debkanchan
Copy link
Author

@cranesandcaff can you provide any repro cause all my tests seem to work

@anda98
Copy link

anda98 commented Feb 14, 2024

Hi!
I've tried using your patterns with a slight change in the decimal precision, as such:

export const PATTERN_LATITUDE_WITH_PRECISION = /^-?([0-8]?[0-9]|90)(\.[0-9]{1,8})?$/gmu;
export const PATTERN_LONGITUDE_WITH_PRECISION = /^-?([0-9]{1,2}|1[0-7][0-9]|180)(\.[0-9]{1,8})?$/gmu;

I'm using these two constants to validate inputs in 2 form controls in Typescript:

longitude: new FormControl<number | undefined>(undefined, [Validators.pattern(PATTERN_LONGITUDE_WITH_PRECISION)]),
latitude: new FormControl<number | undefined>(undefined, [Validators.pattern(PATTERN_LATITUDE_WITH_PRECISION)]),

For some reason, regardless of the patterns I use, the online compilers for regex patterns validate and invalidate correctly any examples I provide, whereas, in my form, the fields trigger errors for examples like:

  • 56.38472 for longitude
  • -1 for latitude

Do you have any idea why this is?

@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