Solved

VB.Net WinForm Validation ErrorProvider regular expression

Posted on 2011-03-03
4
1,838 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
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.
The viewer will learn how to set up a document for the web and print and the recommended PPI for printing.
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.

758 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