Reputation: 949
I am trying to get the HTML5 input pattern attribute to validate a latitude longitude string as follows: [29.71620257395901,-95.42931508327673]
Cannot seem to get the expression quiet right...
<input type="text" name="latlon" pattern="/\[[+-]?([0-9]*[.])?[0-9]+\,[+-]?([0-9]*[.])?[0-9]+$/" />
Upvotes: 1
Views: 2125
Reputation: 226
Latitude should lie between -90 to 90 and Longitude should lie between -180 to 180
So, the following regex will help to validate with latitude and longitude valid ranges
\[(\+|\-|)(([0-8]\d?)(\.\d+)?|90(\.0+)?)\,(\+|\-|)((\d?\d|1[0-7]\d)(\.\d+)?|180(\.0+)?)\]
Test Cases
[89.438483,67.348389] - Valid
[90.39393,178.000] - Invalid
[0.347347,78.49494] - Valid
[-20.5858,90.889] - Valid
[-78,-179] - Valid
[-78.48348,180.00] - Valid
[-78.384844,180.44] - Invalid
Regex Demo - https://regexr.com/61rnl
Upvotes: 3
Reputation: 163207
You could use
\[[+-]?[0-9]*\.?[0-9]+,[+-]?[0-9]*\.?[0-9]+]
Note that with all the optional parts, it can also match .42931508327673
Upvotes: 1