Solved

VB.Net WinForm Validation ErrorProvider regular expression

Posted on 2011-03-03
4
1,904 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Curious about the latest ransomware attack? Check out our timeline of events surrounding the spread of this new virus along with tips on how to mitigate the damage.
This video shows how use content aware, what it’s used for, and when to use it over other tools.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

627 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