Number and Alphabets Validation in VB.Net


Fig: Design
Source Code:

Public Class Validation

    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
       
  If (IsNumeric(e.KeyChar()) Or (Asc(e.KeyChar()) = 8)) Then
            e.Handled = False

        Else
            e.Handled = True
            MsgBox("Enter only digits")
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (String.IsNullOrEmpty(TextBox1.Text)) Then
            ErrorProvider1.SetError(TextBox1, "Required field")
        End If
    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        If (((Asc(e.KeyChar()) > 64 And Asc(e.KeyChar()) < 92) Or (Asc(e.KeyChar()) > 96 And Asc(e.KeyChar()) < 123))) Then
            e.Handled = False
        Else
            e.Handled = True
            ErrorProvider1.SetError(TextBox2, "Only alphabets")
            MsgBox("Only Alphabets")
        End If
    End Sub
End Class

Comments

Popular posts from this blog

Silde show of images In JSP

Compression ratio, Compression factor, saving percentage in C++

Working with database in Vb.net