validate latitude and longitude c# image

Validation for latitude and longitude values

Latitude and longitude are basic geographic terms that determine location coordinate of any place located on earth. They are similar to the fingerprint and just like two people cannot have the same fingerprint, two different geographic locations cannot ever share common coordinates. This article demonstrates how to validate latitude and longitude value in JavaScript C# ASP.Net.

  • Latitude: Latitude is a geographic coordinate that specifies the north-south position of a point on the Earth.
  • Longitude: latitude is a geographic coordinate that specifies the east-west position of a point on the Earth.
  • Altitude: it is the height of the object from sea level

To implement validation for latitude and longitude values we can write a logic that, latitude must be a number between -90 and 90 and the longitude between -180 and 180 degree inclusive.

  • Validate latitude and longitude JavaScript

In this example, we will learn latitude and longitude validation in JavaScript. Find the code snippet below:-

<script type="text/javascript">
        function ValidateLatLng() {
            var lat = document.getElementById("txtLat").value;
            var lng = document.getElementById("txtLon").value;

            if (lat < -90 || lat > 90) {
                alert("Latitude must be between -90 and 90 degrees inclusive.");
            }
            else if (lng < -180 || lng > 180) {
                alert("Longitude must be between -180 and 180 degrees inclusive.");
            }
            else if (lat == "" || lng == "") {
                alert("Enter a valid Latitude or Longitude!");
            }
        }
    </script>
  • Validate Latitude and Longitude C#

In this example, we will learn latitude and longitude validation in C# asp.net. Find the below source code:-

using System;
public class Validation
{
    double _lat, _lng;
    public Double Lat
    {
        get
        {
            return this._lat;
        }
        set
        {
            if (value < -90 || value > 90)
            {
                throw new ArgumentOutOfRangeException("Latitude must be between -90 and 90
 degrees inclusive.");
            }
            this._lat = value;
        }
    }

    public Double Lng
    {
        get
        {
            return this._lng;
        }
        set
        {
            if (value < -180 || value > 180)
            {
                throw new ArgumentOutOfRangeException("Longitude must be between -180 and 180
 degrees inclusive.");
            }
            this._lng = value;
        }
    }
}


Output

validation for latitude and longitude values demo image validate latitude and longitude How to validate Latitude and Longitude value validation for latitude and longitude values demo

Download Code

What do you think?

I hope you liked this article. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

LEAVE A REPLY

Please enter your comment!
Please enter your name here