Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using regular expression to validate latitude and longitude coordinates then display error [closed]

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>
like image 818
Ryan Avatar asked Apr 07 '14 04:04

Ryan


People also ask

How do you validate the value of latitude and longitude?

Each line contains a pair of co-ordinates which possibly indicate the latitude and longitude of a place. The latitude and longitude, if present will always appear in the form of (X, Y) where X and Y are decimal numbers. For a valid (latitude, longitude) pair: -90<=X<=+90 and -180<=Y<=180.

What is latitude and longitude example?

Latitude and longitude are a pair of numbers (coordinates) used to describe a position on the plane of a geographic coordinate system. The numbers are in decimal degrees format and range from -90 to 90 for latitude and -180 to 180 for longitude. For example, Washington DC has a latitude 38.8951 and longitude -77.0364 .

What is N and E coordinates?

Unlike decimal coordinates the sexagesimal coordinates can not be negative. In their case, the letter W or E is added to the longitude to specify the position east-west from the Greenwich meridian, and the letter N or S to the latitude to designate the hemisphere (North or South).

Can latitude be integer?

The latitude and longitude are displayed as integers in degrees, followed by minutes and seconds.


2 Answers

<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>
like image 112
goodguytrigu Avatar answered Oct 15 '22 06:10

goodguytrigu


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

like image 13
Mosho Avatar answered Oct 15 '22 05:10

Mosho