9

How can I use a regular expression to validate the latitude and longitude for these text fields and display an error message?

 <div class="form_style">
    <fieldset>
      <label for="contentText" class="fixedwidth">Name:</label>
      <input type="text" name="content_txt" id="contentText" class="inputtext" ><br/> 
    </fieldset>
    <fieldset>
      <label for="address" class="fixedwidth">Address:</label>
       <input type="text" id="address"class="inputtext" /><br/>
    </fieldset>
    <fieldset>
       <label for="lat" class="fixedwidth">Lat:</label>
     <input type="text" id="lat"class="inputtext" /><br/>
    </fieldset>
    <fieldset>
      <label for="lng" class="fixedwidth">Lng:</label>
     <input type="text" name="lng" id="lng"class="inputtext" /><br/>
    </fieldset>
    <fieldset>
      <label for="type" class="fixedwidth">Type:</label>
     <input type="text" id="type"class="inputtext" /><br/>
    </fieldset>
    <button id="FormSubmit">Add Marker</button>
</div>
4
  • What would be valid ones?
    – Mosho
    Commented Apr 7, 2014 at 4:45
  • So I think the regex would be something like ^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}
    – Ryan
    Commented Apr 7, 2014 at 4:48
  • 1
    What do you expect valid values to look like? The range for latitude is -90 to +90, longitude is -180 to +180, but maybe greater for administrative boundaries. Also, is +/- or n/s, e/w being used? How about degree (°), minute (') or second (") indicators? How are values delimited: by space, dot, colon, nothing?
    – RobG
    Commented Apr 7, 2014 at 5:00
  • If just testing an integer value, then lat >= -90 && lat <= 90 and long >= -180 && long <= 180 is simpler and less to type than a regular expression.
    – RobG
    Commented Apr 7, 2014 at 5:20

4 Answers 4

19
<script type="text/javascript">  

var latitude = document.getElementById(lat).value;
var longitude = document.getElementById(lng).value;

var reg = new RegExp("^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}");

if( reg.exec(latitude) ) {
 //do nothing
} else {
 //error
}

if( reg.exec(longitude) ) {
 //do nothing
} else {
 //error
}

</script>
3
  • 1
    Shouldn't there be separate regexes for latitude and longitude? -90 to 90 versus -180 to 180? Commented Apr 7, 2014 at 4:58
  • 2
    You are right! I just used the one @Kelv provided. Apparently something like that should work: ^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$ link Didn't tried tho Commented Apr 7, 2014 at 5:01
  • 2
    i used in my code but it went to the else condition of javascript always. mean it's rejecting my any input. Commented Jan 10, 2017 at 13:44
13

Feel free to test your regex here:

^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}

Regular expression visualization

Debuggex Demo

In this case, the regex would match a number that is negative or positive, either (1 digit excluding '0' and a '0') or (one or two digits, the first one excluding 9, both excluding '0'), followed by a decimal point and up to 6 other digits. If that's what you need, then yes, this would work. If you need a different format, post it in a comment and I'll try to work a proper regex.

Googling "regex" should give you all the info you need.

If you just need numbers between -90 and 90 (or -180 and 180) you can just do this:

if (typeof num === 'number' && num <= 90 && num >= -90)
    //valid
else
    //invalid

If you need symbols like ° (degrees) or compass direction, then regex could be beneficial.

Here is a review of a few formats:

http://www.geomidpoint.com/latlon.html

0
1

Just do this:

function isLatitude(lat) {
  return isFinite(lat) && Math.abs(lat) <= 90;
}

function isLongitude(lng) {
  return isFinite(lng) && Math.abs(lng) <= 180;
}
-1

I used two different regexes for each value.

Latiude Regex:

const latRegex = /^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,15}/g;

Longitude Regex:

const lngRegex = /^-?(([-+]?)([\d]{1,3})((\.)(\d+))?)/g;

Not the answer you're looking for? Browse other questions tagged or ask your own question.