Ep

Ep

Latitude and Longitude Validation in C#

Latitude and Longitude validation in Javascript

Let's start a asp.net web application to check that entered latitude and longitude is valid or not. This blog post demonstrates a logic that latitude must be a number between -90 and 90 and the longitude between -180 and 180. Find the below source code snippet to validate latitude and longitude in C# and JavaScript :-
validation to check that it is a valid latidue or longitude, Validate latitude and longitude, C# code, JavaScript, ASP.net
latitude and longitude values in google maps
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>C# - How to check or validate Latitude & Longitude in JavaScript?</title>
    <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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Latitude:
        <asp:TextBox ID="txtLat" runat="server"></asp:TextBox>&nbsp;&nbsp; Longitude:
        <asp:TextBox ID="txtLon" runat="server"></asp:TextBox><br />
        <br />
        <asp:Label ID="lblError" runat="server" Text=""></asp:Label>
        <br />
        <br />
        <asp:Button ID="btnValidate" runat="server" Text="Validate Lat/Lon @ Server Side"
            OnClick="btnValidate_Click" />
        <a href="#" style="text-decoration: none;" onclick="ValidateLatLng();">Validate Lat/Lon
            @ Client Side</a>
    </div>
    </form>
</body>
</html>

Validation.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for Validation
/// </summary>
public class Validation
{
      public Validation()
      {
            //
            // TODO: Add constructor logic here
            //
      }
    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;
        }
    }
}
Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    Validation obj = new Validation();
    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = string.Empty;
    }

    protected void btnValidate_Click(object sender, EventArgs e)
    {
        try
        {
            obj.Lat = Convert.ToDouble(txtLat.Text);
            obj.Lng = Convert.ToDouble(txtLon.Text);
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message; lblError.ForeColor = System.Drawing.Color.Red;
        }
    }
Latitude and Longitude Validation in C# Reviewed by Ravi Kumar on 1:09:00 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.