This site include tutorials of VB.Net programming

Tuesday, April 13, 2010

Input Field validation (Accept only numbers)

  • In this exercise you will learn one of the methods of validating inputs at field level















  • Add a new form to project. Name it as frmTextboxvalid.
  • Place a Label and  a TextBox. Name the textbox as txtNumbers.
  • Click at "View" and then at "Code" to enter the code window.
  • Click at the combo box on the left side and select txtNumbers.
  • Click at the combo box on the right side and select KeyPress event.
  • Then write the following code.
                    Private Sub txtNumbers_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumbers.KeyPress


                          If Asc(e.KeyChar) <> 8 Then
                                   If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
                                          e.Handled = True
                                  End If
                          End If


                   End Sub

  • Run the Application.