Link to home
Start Free TrialLog in
Avatar of rtay
rtayFlag for United States of America

asked on

VB.Net WinForm Validation ErrorProvider regular expression

I would like to validate a text box on a windows form in VB.Net to check first that the required field has a value and then check to make sure it matches the RegEx.  This is a Windows Form and not ASP.  I have been trying ErrorProvider and I can get it to check for a required feild, but not then check the same textbox for the regex.  

Thank you.
Private Sub TruckNoTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TruckNoTextBox.Validating
        Dim Regex As New Regex("^[Y,S,0-9]{1,4}$")
        If Regex.IsMatch(TruckNoTextBox.Text) Then
            ErrorProvider2.SetError(TruckNoTextBox, "Please enter correct format")
        Else
            ErrorProvider2.SetError(TruckNoTextBox, "Wrong")

        End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of rawinnlnx9
rawinnlnx9
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rtay

ASKER

Your validate() soultion did the trick.  Now for the second part, how can I check to make sure the field is not empty first and then if it is not empy check the regex.  Here is the code working for regex.

Private Sub TruckNoTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TruckNoTextBox.Validating
        Dim Regex As New Regex("^[Y,S,0-9]{1,4}$")
        If Regex.IsMatch(TruckNoTextBox.Text) Then
            ErrorProvider2.SetError(TruckNoTextBox, "")
        Else
            ErrorProvider2.SetError(TruckNoTextBox, "Wrong")

        End If
        Me.Validate()

    End Sub

Avatar of rtay

ASKER

got it with an elseif

If TruckNoTextBox.Text = "" Then
End If

I'm comparing it to an empty "" but some prefer instead to use String.Empty