VS.NET - ASP.NET - ASP.NET Numeric Textbox control

ASP.NET Numeric Textbox control that handles pasting.

Synopsis:

A common need I've had when working with ASP.NET is for a numeric only textbox control. The standard control doesn't offer such features. I had been using a Numeric control from Excentrics World, but even this didn't handle pasting exactly as I wanted, nor is it available for download now (for the time being at least.). To do this I used a validation script on mredkj and bundled it into a custom control that inherits from the textbox control and adds a few properties to set parameters used for the javascript. For more information about the javascript to allow only numbers go here: JavaScript validate 'as you type'

Solution:

Download : Dll and Code


Option Explicit On Imports System.ComponentModel Imports System.Web.UI Public Class NumericTextBox Inherits System.Web.UI.WebControls.TextBox Private _DecimalPlaces As Int32 = -1 Private _AllowDecimal As Boolean Private _AllowNegative As Boolean Private js As String = "<script language='javascript'>// created: 2005-08-30" & vbCrLf & _ "// updated: 2005-08-31" & vbCrLf & _ "// mredkj.com" & vbCrLf & _ "function extractNumber(obj, decimalPlaces, allowNegative)" & vbCrLf & _ "{" & vbCrLf & _ " var temp = obj.value;" & vbCrLf & _ " " & vbCrLf & _ " // avoid changing things if already formatted correctly" & vbCrLf & _ " var reg0Str = '[0-9]*';" & vbCrLf & _ " if (decimalPlaces > 0) {" & vbCrLf & _ " reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';" & vbCrLf & _ " } else if (decimalPlaces < 0) {" & vbCrLf & _ " reg0Str += '\\.?[0-9]*';" & vbCrLf & _ " }" & vbCrLf & _ " reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;" & vbCrLf & _ " reg0Str = reg0Str + '$';" & vbCrLf & _ " var reg0 = new RegExp(reg0Str);" & vbCrLf & _ " if (reg0.test(temp)) return true;" & vbCrLf & _ "" & vbCrLf & _ " // first replace all non numbers" & vbCrLf & _ " var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') " & _ "+ (allowNegative ? '-' : '') + ']';" & vbCrLf & _ " var reg1 = new RegExp(reg1Str, 'g');" & vbCrLf & _ " temp = temp.replace(reg1, '');" & vbCrLf & _ "" & vbCrLf & _ " if (allowNegative) {" & vbCrLf & _ " // replace extra negative" & vbCrLf & _ " var hasNegative = temp.length > 0 && temp.charAt(0) == '-';" & vbCrLf & _ " var reg2 = /-/g;" & vbCrLf & _ " temp = temp.replace(reg2, '');" & vbCrLf & _ " if (hasNegative) temp = '-' + temp;" & vbCrLf & _ " }" & vbCrLf & _ " " & vbCrLf & _ " if (decimalPlaces != 0) {" & vbCrLf & _ " var reg3 = /\./g;" & vbCrLf & _ " var reg3Array = reg3.exec(temp);" & vbCrLf & _ " if (reg3Array != null) {" & vbCrLf & _ " // keep only first occurrence of . " & vbCrLf & _ " // and the number of places specified by decimalPlaces " & _ "or the entire string if decimalPlaces < 0" & vbCrLf & _ " var reg3Right = temp.substring(reg3Array.index " & _ "+ reg3Array[0].length);" & vbCrLf & _ " reg3Right = reg3Right.replace(reg3, '');" & vbCrLf & _ " reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;" & vbCrLf & _ " temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;" & vbCrLf & _ " }" & vbCrLf & _ " }" & vbCrLf & _ " " & vbCrLf & _ " obj.value = temp;" & vbCrLf & _ "}" & vbCrLf & _ "function blockNonNumbers(obj, e, allowDecimal, allowNegative)" & vbCrLf & _ "{" & vbCrLf & _ " var key;" & vbCrLf & _ " var isCtrl = false;" & vbCrLf & _ " var keychar;" & vbCrLf & _ " var reg;" & vbCrLf & _ " " & vbCrLf & _ " if(window.event) {" & vbCrLf & _ " key = e.keyCode;" & vbCrLf & _ " isCtrl = window.event.ctrlKey" & vbCrLf & _ " }" & vbCrLf & _ " else if(e.which) {" & vbCrLf & _ " key = e.which;" & vbCrLf & _ " isCtrl = e.ctrlKey;" & vbCrLf & _ " }" & vbCrLf & _ " " & vbCrLf & _ " if (isNaN(key)) return true;" & vbCrLf & _ " " & vbCrLf & _ " keychar = String.fromCharCode(key);" & vbCrLf & _ " " & vbCrLf & _ " // check for backspace or delete, or if Ctrl was pressed" & vbCrLf & _ " if (key == 8 || isCtrl)" & vbCrLf & _ " {" & vbCrLf & _ " return true;" & vbCrLf & _ " }" & vbCrLf & _ " reg = /\d/;" & vbCrLf & _ " var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;" & vbCrLf & _ " var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;" & vbCrLf & _ " " & vbCrLf & _ " return isFirstN || isFirstD || reg.test(keychar);" & vbCrLf & _ "}</script>" Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs) MyBase.OnPreRender(e) Dim decPlaces As Int32 If _AllowDecimal Then decPlaces = _DecimalPlaces End If Me.Page.RegisterClientScriptBlock("NumericValidation", js) Me.Attributes.Add("onblur", "extractNumber(this," & _ decPlaces & "," & _AllowNegative.ToString.ToLower & ");") Me.Attributes.Add("onkeyup", "extractNumber(this," & _ decPlaces & "," & _AllowNegative.ToString.ToLower & ");") Me.Attributes.Add("onkeypress", "return blockNonNumbers(this, event," & _ _AllowDecimal.ToString.ToLower & "," & _AllowNegative.ToString.ToLower & ");") End Sub Public Property DecimalPlaces() As Int32 Get Return _DecimalPlaces End Get Set(ByVal Value As Int32) _DecimalPlaces = Value End Set End Property Public Property AllowDecimal() As Boolean Get Return _AllowDecimal End Get Set(ByVal Value As Boolean) _AllowDecimal = Value End Set End Property Public Property AllowNegative() As Boolean Get Return _AllowNegative End Get Set(ByVal Value As Boolean) _AllowNegative = Value End Set End Property End Class

Examples of Use:
	NumericTextBox1.AllowDecimal = True
        NumericTextBox1.AllowNegative = False
	NumericTextBox1.DecimalPlaces = 2

Additional Information:
A DecimalPlaces value of -1 = unlimited numbers after the decimal point
About this page: