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>
Jerry
  • 70,495
  • 13
  • 100
  • 144
Ryan
  • 107
  • 1
  • 1
  • 6
  • What would be valid ones? – Mosho Apr 07 '14 at 04:45
  • So I think the regex would be something like ^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6} – Ryan Apr 07 '14 at 04: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 Apr 07 '14 at 05: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 Apr 07 '14 at 05:20

4 Answers4

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>
goodguytrigu
  • 456
  • 2
  • 13
  • 1
    Shouldn't there be separate regexes for latitude and longitude? -90 to 90 versus -180 to 180? – Jason Aller Apr 07 '14 at 04:58
  • 2
    You are right! I just used the one @Kelv provided. Apparently something like that should work: ^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$ [link](http://stackoverflow.com/questions/3518504/regular-expression-for-matching-latitude-longitude-coordinates) Didn't tried tho – goodguytrigu Apr 07 '14 at 05:01
  • 2
    i used in my code but it went to the else condition of javascript always. mean it's rejecting my any input. – Divyesh Jesadiya Jan 10 '17 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

Mosho
  • 7,099
  • 3
  • 34
  • 51
1

Just do this:

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

function isLongitude(lng) {
  return isFinite(lng) && Math.abs(lng) <= 180;
}
Bhad Guy
  • 80
  • 7
-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;

Luis Sandoval
  • 75
  • 2
  • 9