?
Solved

VB.Net WinForm Validation ErrorProvider regular expression

Posted on 2011-03-03
4
Medium Priority
?
1,922 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . If you have responsibility for software in production, I bet you’d like to know more about it. I don’t mean that you’d like an extra peek into the bowels of the sourc…
In this article I discuss my selections of the Top Four free Outlook OST File Viewers available. Open, view and read even damaged OST files by using these tools. They all provide a clear preview of all data such as emails, notes, tasks, calendars, e…
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

719 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