0

Could someone show some guidance(am not asking to do my homework) with validating a form with multiple text boxes? User would be informed what was the problematic field.

The source of the form:

Private Sub btnNewUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewUser.Click
  'If txtEmail.Text.Contains(" "c) Or Not(InStr(txtEmail.Text, "@")) Then
  'txtEmail.Clear()
  'ElseIf txtPassword.Text.Contains(" "c) Then
  'txtPassword.Clear()
  'ElseIf txtPIN.Text ''#uh
    aryUserRecord(0) = txtEmail.Text
    aryUserRecord(1) = txtPassword.Text
    aryUserRecord(2) = txtPIN.Text ''#consists of a letter then two numbers then another                     addNewUser = Join(aryUserData, ",")
   ''#more source
    Me.DialogResult = DialogResult.OK
End Sub
4
  • What kind of validation? Commented Dec 1, 2010 at 15:53
  • Validate if user inputted right information into the three text boxes. If any of them has wrong information then inform user upon clicking on the button what was the problem text box, clear it and let user input the info again for it. Commented Dec 1, 2010 at 15:58
  • 1
    OK. What does "right information" mean? What does "wrong information" mean? Figure that out first. Commented Dec 1, 2010 at 16:00
  • Well, email field would have to contain the "@" atleast. Password can contain anything but spaces and PIN has to have a structure of a letter, 2numbers and then letter again(A12B). That's the right informations otherwise it is wrong. A msgbox should popup and tell user what are the fields with wrong information. Commented Dec 1, 2010 at 16:02

2 Answers 2

5

You can use an ErrorProvider to mark the problematic fields. You'll want to hook up with the validating event for each TextBox. Something like this:

Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        If TextBox1.Text = "" Then
            ErrorProvider1.SetError(TextBox1, "Text cannot be empty")
            e.Cancel = True
        End If
End Sub

Then when the Textbox does actually validate, you can hook up to the Validated event to clear out the ErrorProvider:

Private Sub TextBox1_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Validated
        ErrorProvider1.SetError(TextBox1, "")
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

It highlights ErrorProvider1. What do I declare it as?
ErrorProvider is a control that you can drop on your form in the designer. It will make a little mark next to your problematic fields.
0

Try reading up on RegularExpressionValidator.

You could have one assigned for each textbox to validate user input client side using regular expressions, which based on your comments to the question seem like a good choice.

with winforms you'll need to implement Validating and Validated events

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation.aspx

in the link above an example is provided for e-mail. This should give you a reference to start

1 Comment

All the info comes up for ASP.net for some reason. I'm not trying to use this program for web application. It's an offline validation program with no databases or anything like that. The real issue at hand is how I make it so a msgbox that appears will actually point out the field that is wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.