I have textboxes that I only want to allow integers. One thing on these textboxes is I have OnTextChange to do calculations. Now the thing is if the user puts anything else other than an integer I get an error. Is the a validator or something that will not
allow the user to put something else.
Preferably you'd want to use Javascript for this. (I don't have any handy at the moment) If you're using Visual Basic, you can use the IsNumeric() function on the server-side to make sure the value is an integer.
"I would love to change the world, but they won't give me the source code." -unknown
first of all, if user must always enter something, assign a RequiredFieldValidator to the TextBox.
<asp:RequiredFieldValidator ID="ReqVal1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Input is required" />
Then to make sure that only integers are allowed, you can utilize CompareValidator (it won't run if user hasn't entered anything, therefore RequiredFieldValidator is also needed)
Public Sub New(ByVal Value As Double)
MyBase.New()
Me.Text = Value
End Sub
Enum DataTypes
dtAny
dtInteger
dtDouble
End Enum
Private mDataType As DataTypes
Public Property DataType() As DataTypes
Get
Return mDataType
End Get
Set(ByVal Value As DataTypes)
mDataType = Value
End Set
End Property
Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal Value As String)
Select Case Me.DataType
Case DataTypes.dtAny
MyBase.Text = Value
Case DataTypes.dtDouble
Try
MyBase.Text = _
System.Convert.ToDouble(Value)
Catch
MyBase.Text = 0
End Try
Case DataTypes.dtInteger
Try
MyBase.Text = _
System.Convert.ToInt32(Value)
Catch
MyBase.Text = 0
End Try
End Select
End Set
End Property
Private Sub NumericTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If Me.DataType = DataTypes.dtInteger Then
If Not (e.KeyChar.IsDigit(e.KeyChar) Or _
e.KeyChar = ControlChars.Tab Or _
e.KeyChar = ControlChars.Back) Then
e.Handled = True
End If
ElseIf Me.DataType = DataTypes.dtDouble Then
If Not (e.KeyChar.IsDigit(e.KeyChar) Or _
e.KeyChar = ControlChars.Tab Or _
e.KeyChar = ControlChars.Back Or _
e.KeyChar = ".") Then
e.Handled = True
End If
End If
etg1103
Participant
905 Points
189 Posts
textbox to allow only integers
Aug 12, 2005 10:01 PM|LINK
FrankWhite
Contributor
2690 Points
538 Posts
Re: textbox to allow only integers
Aug 13, 2005 01:33 AM|LINK
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: textbox to allow only integers
Aug 13, 2005 06:21 PM|LINK
Hi,
first of all, if user must always enter something, assign a RequiredFieldValidator to the TextBox.
<asp:RequiredFieldValidator ID="ReqVal1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Input is required" />
Then to make sure that only integers are allowed, you can utilize CompareValidator (it won't run if user hasn't entered anything, therefore RequiredFieldValidator is also needed)
<asp:CompareValidator ID="intVal1" runat="server" ControlToValidate="TextBox1" Operator="DataTypeCheck" Type="Integer" ErrorMessage="Only integers are allowed" />
(If there's the chance that browser is not IE or doesn't support client-side validation, also check Page.IsValid at server-side)
There's also commercial control, Peter Blum's IntegerTextBox
Teemu Keiski
Finland, EU
smiling4ever
All-Star
15825 Points
3123 Posts
Re: textbox to allow only integers
Aug 13, 2005 08:10 PM|LINK
This class will help you well![Smile [:)]](/emoticons/emotion-1.gif)
Public Class NumericTextBox
Inherits System.Windows.Forms.TextBox
Public Sub New()
MyBase.New()
Me.Text = 0
End Sub
Public Sub New(ByVal Value As Double)
MyBase.New()
Me.Text = Value
End Sub
Enum DataTypes
dtAny
dtInteger
dtDouble
End Enum
Private mDataType As DataTypes
Public Property DataType() As DataTypes
Get
Return mDataType
End Get
Set(ByVal Value As DataTypes)
mDataType = Value
End Set
End Property
Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal Value As String)
Select Case Me.DataType
Case DataTypes.dtAny
MyBase.Text = Value
Case DataTypes.dtDouble
Try
MyBase.Text = _
System.Convert.ToDouble(Value)
Catch
MyBase.Text = 0
End Try
Case DataTypes.dtInteger
Try
MyBase.Text = _
System.Convert.ToInt32(Value)
Catch
MyBase.Text = 0
End Try
End Select
End Set
End Property
Private Sub NumericTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If Me.DataType = DataTypes.dtInteger Then
If Not (e.KeyChar.IsDigit(e.KeyChar) Or _
e.KeyChar = ControlChars.Tab Or _
e.KeyChar = ControlChars.Back) Then
e.Handled = True
End If
ElseIf Me.DataType = DataTypes.dtDouble Then
If Not (e.KeyChar.IsDigit(e.KeyChar) Or _
e.KeyChar = ControlChars.Tab Or _
e.KeyChar = ControlChars.Back Or _
e.KeyChar = ".") Then
e.Handled = True
End If
End If
End Sub
End Class
tim.guo
Member
5 Points
1 Post
Re: textbox to allow only integers
Aug 14, 2005 03:27 AM|LINK
web base application u can do this:
/*======================================================================*/
string clientScript;
clientScript="if ( !( (window.event.keyCode > 47) && (window.event.keyCode <= 57))) { window.event.keyCode=0; }";
this._txtBox.Attributs.add("keypress",clientScript);
/*======================================================================*/