Hi.. I'm doing a project on Vb.net 2008.. There's a textbox called "Price" which requires validation.. The validation is that the textbox should not enter dot, special characters or alphabets as the first character.. The Dot should only be active after the number which is entered on the textbox.. for now i'm using this code.. Thanks in advance

Private Sub txt7_pprice_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt7_pprice.KeyPress

        If Char.IsDigit(e.KeyChar) Or (Asc(e.KeyChar) = Asc(".")) And Me.txt7_pprice.Text.Count(Function(c As Char) c = ".") = 0 Then
            e.Handled = False
        ElseIf Asc(e.KeyChar) = 8 Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

Use this, its very simple.

 If txtPrice.Text.StartWith(".") Or Char.IsLetter(txtPrice.Text) Then
 ' The entered values are not allowed here, either a user entered a "." Or a user entered a letter from (a-z)

 Else
 ' The user entered allowed values here

 End if

This is very simple. Hope this helps you.

txtPrice.Text.StartWith should be: txtPrice.Text.StartsWith

Yes thanks, typed that while I was in a hurry.

You could do

Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Select Case e.KeyChar

        Case "0" To "9"
        Case "."
            Dim tbx As TextBox = sender
            e.Handled = tbx.Text = "" Or InStr(tbx.Text, ".") > 0
        Case Else
            Dim key As Integer = Asc(e.KeyChar)
            e.Handled = Not (key = Keys.Back Or key = Keys.Delete)

    End Select

End Sub

The first Case allows digits. The second case allows only one decimal point as long as it isn't the only character. The third case allows the editing keys, backspace and delete.

What it won't prevent is someone adding a leading decimal by pressing Home and inserting a decimal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.