textbox validation

kaddu747

New member
Joined
Sep 30, 2005
Messages
4
Programming Experience
1-3
So I'm making a form where I need to have the textbox input as
  • a required field - so can't be blank
  • must be numeric
  • must be a positive number
  • must also be an integer
I have the validation down to check if the input is numeric and if it's positive...how to do an integer and entry check...

im not sure i can use the "like" compare method...

VB.NET:
PublicSharedFunction PositiveInteger(ByVal UserTextBox As Control, ByVal FieldName AsString) AsBoolean
 
Dim bValid AsBoolean
 
   If IsNumeric(UserTextBox.Text) Then
            If UserTextBox.Text > 0 Then
            bValid = True
            Else
               MessageBox.Show(FieldName & " must be positive.")
               UserTextBox.Focus()
               bValid = False
            EndIf
   Else
     MessageBox.Show(FieldName & " must be numeric.")
     UserTextBox.Focus()
     bValid = False
   EndIf
Return bValid

 
You could try this:

VB.NET:
        If Not TextBox1.Text = "" Then
            If IsNumeric(TextBox1.Text) Then
                If TextBox1.Text > 0 Then
                    Try
                        Dim string1 As String = TextBox1.Text
                        Dim int1 As Integer
                        int1 = Convert.ToInt32(string1)
                        MsgBox(int1)
                    Catch ex As Exception
                        MsgBox("non-integer")
                    End Try

                End If
            Else

            End If
        Else

        End If

1st: Check for "" (blank)
2nd: Check for numeric
3rd: Check for > 0
4th: Convert to an integer and catch the exception if it is not able to convert to an integer.

Hope that helps.
 
You can try this personal control, compile it, right click on your windows form toolbox to add it by selecting the file you have created in your solution (copy and paste won't work as I don't use the keydown event).

You then only to drag it on your form as a textbox and check for the empty field when validating.

VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2][COLOR=#000000] IntegerBox[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][SIZE=2] TextBox
 

[/SIZE][SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overrides[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OnCreateControl()[/SIZE][INDENT][SIZE=2][COLOR=#008000]'To avoid initial value at design
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].OnCreateControl()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Clear()
[/SIZE]

[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
 

[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overrides[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OnKeyPress([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyPressEventArgs)
 

[/SIZE][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] e.KeyChar[/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]CChar[/COLOR][/SIZE][SIZE=2]("0") [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]CChar[/COLOR][/SIZE][SIZE=2]("9")[/SIZE][INDENT][SIZE=2][COLOR=#008000]'I have read it is faste than Char.IsDigit
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'No treatment good entry
[/COLOR][/SIZE][SIZE=2]e.Handled = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE]

[/INDENT][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] ControlChars.Back, ControlChars.Cr, ControlChars.Tab[/SIZE][INDENT][SIZE=2][COLOR=#008000]'Allow necessary controls to work
[/COLOR][/SIZE][SIZE=2]e.Handled = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE]

[/INDENT][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Else
 

[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#008000]'Bad entry, refuse it
[/COLOR][/SIZE][SIZE=2]e.Handled = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]Beep()
[/SIZE]

[/INDENT][/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Select
[/COLOR][/SIZE]

[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
 

[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overrides[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OnLostFocus([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs)[/SIZE][INDENT][SIZE=2][COLOR=#008000]'To display number with thousand separator
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text.Length > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
 

[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Try
 

[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Number [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text = Format(Number, "##,##0")
[/SIZE]

[/INDENT][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[INDENT]MsgBox(ex.Message)
 

[/INDENT][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE]

[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]

[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
 

[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overrides[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OnGotFocus([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs)[/SIZE][INDENT][SIZE=2][COLOR=#008000]'To cancel thousand separator and allow modification
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text.Length > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
 

[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Try
 

[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Number [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Text = Number.ToString
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].SelectAll()
[/SIZE]

[/INDENT][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[INDENT]MsgBox(ex.Message)
 

[/INDENT][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE]

[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]

[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
 
integers are whole numbers, only allow numbers (0 through 9) to be entered, you can do this is the keypress event

which means it'll be a numeber (no letters), it'll be positive (the - (dash) isnt allowed to be entered), it'll be integer (the . (period) isnt allowed either)

then in the leave event you can check to see if it's "" (empty) or not
 
Back
Top