(function($)
	{
		$.fn.constrainInput = function(config)
		{
			var settings = $.extend(
				{  
					allowedCharsRegex: ".*", // use "\\d+" for integers only
					maxInt:	false,
					allowEnter: true,
					allowArrows: true,
					allowTab: true,
					invalidCallback: false
				}, config);  
			var re = new RegExp(settings.allowedCharsRegex);
			$.each(this, function()
				{
					var input = $(this);
					
					var value_before = $(this).val();
					
					this.keypressEvent = function(e)
					{
						e= e || window.event;  
						var k = e.charCode || e.keyCode || e.which;
						
						if (settings.allowEnter && k == 13){
							return true;
						} else if (settings.allowTab && k == 9) {
							return true;
						} else if (settings.allowArrows && k >=37 && k <= 40) {
							return true;
						} else if(e.ctrlKey || e.altKey || k == 8){//Ignore  
							return true;  
						} else if ((k >= 41 && k <= 122) ||k == 32 || k > 186){//typeable characters  
							if (re.test(String.fromCharCode(k)))
								return true;
							else {
								if (settings.invalidCallback != false)
									settings.invalidCallback();
								return false;
							}
						}  
						
						if (settings.invalidCallback != false)
							settings.invalidCallback();
						
						return false;  
					}
					
					this.keyupEvent = function(e)
					{
						if (settings.maxInt != false)
						{
							if ($(this).val() > settings.maxInt)
							{
								$(this).val(value_before);
							}
						}
						
						value_before = $(this).val();
						
						return true;  
					}
					
					input.bind("keypress",this.keypressEvent);
					input.bind("keyup",this.keyupEvent);
				});  
			return this;  
		};  
	})(jQuery);  
