Solved

VB.Net WinForm Validation ErrorProvider regular expression

Posted on 2011-03-03
4
1,845 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:rtay
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
rawinnlnx9 earned 500 total points
ID: 35030103
In the controls properties you have set

CausesValidation=true

That's going to fire your validation. Somewhere in your code you need to call Form or panel or whatever .Validate().

Is your validation routine executing and the RegEx is just failing?
0
 
LVL 5

Author Comment

by:rtay
ID: 35030659
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

0
 
LVL 5

Author Comment

by:rtay
ID: 35032420
got it with an elseif

0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 35032449
If TruckNoTextBox.Text = "" Then
End If

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
The viewer will learn how to create multiple layers to apply various filters and how to delete areas from each layer’s filter.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now