Click here to Skip to main content
15,891,621 members
Articles / Web Development / ASP.NET

Latitude/longitude input control: an ASP.NET 2.0 server control

Rate me:
Please Sign up or sign in to vote.
4.07/5 (7 votes)
6 Mar 2006CPOL2 min read 53.8K   748   26  
Latitude longitude input control - as ASP.NET 2.0 server control.
function NumericInputClass()
{
	function NumericInputKillExtraDigits(Val, Type, DecimalSeparator)
	{
		if(Type == 0)
			return(Val);
		if(Type == 1)
			return(Val);
		var DotPos = Val.indexOf(DecimalSeparator);
		if(DotPos == -1)
			return(Val+DecimalSeparator+"00");
		Val += "00";
		return(Val.slice(0, DotPos+3));
	}

	function FixData(This, Type, DecimalSeparator)
	{
		var Val = This.value;
		var NewVal = "";
		var GotDot = false;
		for(var i=0; i<Val.length; i++)
		{
			var c=Val.charAt(i);
			if(c == "-" && i == 0)
			{
				NewVal += c;
				continue;
			}
			// window.alert("new: '"+NewVal+"' c: '"+c+"'");
			if(c == DecimalSeparator)
			{
				if(Type == 0)
				{
					This.value = NewVal;
					return;
				}
				if(GotDot == true)
				{
					This.value = NumericInputKillExtraDigits(NewVal, Type, DecimalSeparator);
					return;
				}
				NewVal += c;
				GotDot = true;
				continue;
			}
			if(c >= '0' && c <= '9')
				NewVal += c;
		}
		This.value = NumericInputKillExtraDigits(NewVal, Type, DecimalSeparator);
	}
	this.FixData = FixData;
	
	function Check(Type, DecimalSeparator)
	{
		var Obj = event.srcElement;
		var Val = Obj.value;
		var Key = event.keyCode;
		var KeyAscii = String.fromCharCode(Key);
		if(KeyAscii == "-")
		{
			if(Obj.value != "")
			{
				event.returnValue = false;
				return;
			}
			return;
		}
		switch(Type)
		{
			case 0:		// integer
				if(KeyAscii < '0' || KeyAscii > '9')
				{
					event.returnValue = false;
					return;
				}
				return;

			case 1:		// Float
			case 2:		// Currency
				var DotPos = Val.indexOf(DecimalSeparator);
				if(KeyAscii == DecimalSeparator)
				{
					if( DotPos >= 0)
					{
						event.returnValue = false;
						return;
					}
					return;
				}
				if(KeyAscii < '0' || KeyAscii > '9')
				{
					event.returnValue = false;
					return;
				}
				return;
		}
	}
	this.Check = Check;
}
var NumericInput = new NumericInputClass();

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions