I've been digging around for a way to validate a list of latitude/longitude pairs. Although I've found some good examples on how to validate pairs and how to validate a list of pairs, I have been unable to write a regex to match my specific requirements. The requirements are as follows:
- Every latitude/longitude pair must contain valid values (solved from the first link)
- Every pair must be separated from other pairs by a comma. The second link uses a semicolon, but replacing this with a comma causes some problems in the regex
- There must be an even number of coordinates so that every coordinate is paired up
- White space after commas is ok
- At least 3 coordinate pairs are required so that the coordinates form a polygon (but we don't need to worry about duplicate coordinates), and the final coordinate pair should not be followed by a comma
The following entries should be valid:
32.3078, 64.7505,
27.6648, 81.5158,
18.2208, 66.5901
32.3078, 64.7505,
27.6648, 81.5158,
18.2208, 66.5901,
32.3078, 64.7505,
27.6648, 81.5158,
18.2208, 66.5901
32.3078,64.7505,27.6648,81.5158,18.2208,66.5901
While these should be invalid:
//only 1 pair
32.3078, 64.7505
//no commas separating each pair
32.3078, 64.7505
27.6648, 81.5158
18.2208, 66.5901
//odd number of pairs
32.3078, 64.7505,
27.6648, 81.5158,
18.2208, 66.5901,
32.3078, 64.7505,
27.6648, 81.5158,
18.2208
//comma after the final pair
32.3078, 64.7505,
27.6648, 81.5158,
18.2208, 66.5901,