Community discussion forum

VB Newby question

  • 2 years ago
    Sorry to all you pro's out there. I need to validate some data in a text box. Very easy I guesss, but I've tried loads of things and am stumped. All I need to do is ensure the user enters text in one TextBox and numbers in another TextBox. How do I do this? Many thanks.
  • 2 years ago

    There are a few ways of doing this, here's one:

    Imports System.Text.RegularExpressions

    Public Class Form1

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    Dim num As Integer

    Dim valid As Boolean

    Try

    num = System.Convert.ToDecimal(TextBox1.Text)

    valid = True

    Catch ex As Exception

    valid = False

    End Try

    Button1.Enabled = valid

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    Dim valid As Boolean

    Dim reg As New Regex("[0-9]")

    Dim match As Match = reg.Match(TextBox2.Text)

    valid = Not match.Success

    Button1.Enabled = valid

    End Sub

    End Class

    HTH

     

  • 2 years ago

    Try testing IsNumeric(TextBox1.Text) for true or false. The following examples also checked out OK for me....

    MsgBox(IsNumeric(

    "123")) 'returns True

    MsgBox(IsNumeric(

    "123.456")) 'returns True

    MsgBox(IsNumeric(

    "123 hello")) 'returns False

    Good Luck

     

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback