How to validate a form with multiple text-boxes...

...and receive a message identifying the problematic text-boxes?

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 16853 Views - Last Post: 14 December 2010 - 10:47 AM Rate Topic: -----

#1 ravencode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-December 10

How to validate a form with multiple text-boxes...

Posted 01 December 2010 - 12:09 PM

Could someone provide some insight?
The form has First Name, Last Name, Course and Email text box fields.
First/Last Name must have no spaces, Course must consist of a three letters and two numbers in that order and nothing else(I want to avoid the use of masked text box), and Email must have the "@" sign and a dot.
How would one make it so after clicking, say, Submit button and if there was a violation of any of the above rules then message box should pop up and tell the user which fields contain incorrect information.

Private Sub buttonAddNewStudent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonAddNewStudent.Click
'validation must be performed before the corresponding text box items will be placed inside an array
    arrayRecord(0) = textFirstName.Text
    arrayRecord(1) = textLastName.Text
    arrayRecord(2) = textCourse.Text
arrayRecord(3) = textEmail.Text
    arrayAddNewStudent = Join(arrayRecord, ",")
    Me.DialogResult = DialogResult.OK
End Sub

This post has been edited by ravencode: 01 December 2010 - 12:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to validate a form with multiple text-boxes...

#2 bflosabre91  Icon User is offline

  • go sabres

Reputation: 105
  • View blog
  • Posts: 1,439
  • Joined: 22-February 08

Re: How to validate a form with multiple text-boxes...

Posted 01 December 2010 - 01:23 PM

here is similar to how i do this:

'in the click event
Dim str As String = ""
str = IsValid()
If str = "" Then
'Its all valid so do w/e here
Else
msgbox("The following fields are invalid: " & str)
End If

'the IsValid Sub. add If statements for the fields you want to validate
Private Function IsQuickAdmissionValid() As String
'return string value containing the missing pieces if it is invalid. else, return ""
 Dim result As String = ""
 If txtLastName.Text.Length = 0 Then
   result = result & "Last Name" & vbNewLine
 End If
Return result
End Sub



i didnt test this code exactly but hopefully it will push you in the right direction.

This post has been edited by bflosabre91: 01 December 2010 - 01:25 PM

Was This Post Helpful? 1
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to validate a form with multiple text-boxes...

Posted 01 December 2010 - 04:59 PM

Is this something your just wanting to do or is it related to a class project? If it's a class project, are you allowed to use regular expressions because it would be perfect for this type of validation.

I'll admit, I'm not very well versed in them as I've not taken the time to mess with them but I do know that you can validate these types of situations with them and that there are a lot of knowledgeable people that frequent this forum that can help you with them.

If that isn't the way you want to go you could simply use .Contains and IndexOf to test these conditions. For example

If FirstNameTextBox.Text.Contains(" "c) then
 Messagebox.Show("Spaces not allowed")
End If


Or
If EmailTextBox.Text.IndexOf("@"c) = -1 Then
 MessageBox.Show("Email invalid, email format should be user@domain(.com)")
End If


I post these because some classes do not want you to go out of the chapter methods.

This post has been edited by CharlieMay: 01 December 2010 - 05:03 PM

Was This Post Helpful? 1
  • +
  • -

#4 xnn  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 227
  • Joined: 10-February 10

Re: How to validate a form with multiple text-boxes...

Posted 01 December 2010 - 06:24 PM

Forms and input controls have a validating event that you can trigger. That way it's validating when you hit submit instead of validating on keypress or any other control input or on/off focus.
Was This Post Helpful? 1
  • +
  • -

#5 ravencode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-December 10

Re: How to validate a form with multiple text-boxes...

Posted 07 December 2010 - 11:50 AM

View Postbflosabre91, on 01 December 2010 - 12:23 PM, said:

here is similar to how i do this:

'in the click event
Dim str As String = ""
str = IsValid()
If str = "" Then
'Its all valid so do w/e here
Else
msgbox("The following fields are invalid: " & str)
End If

'the IsValid Sub. add If statements for the fields you want to validate
Private Function IsQuickAdmissionValid() As String
'return string value containing the missing pieces if it is invalid. else, return ""
 Dim result As String = ""
 If txtLastName.Text.Length = 0 Then
   result = result & "Last Name" & vbNewLine
 End If
Return result
End Sub



i didnt test this code exactly but hopefully it will push you in the right direction.


For some reason if I made a mistake in each field, error message informs me only of illegal characters in Phone field and GPA being out of bound. What went wrong here?

Source(made some changes to the code):

Public Class frmAddNewStudent
    Dim aryData(6) As String
    Public Shared newStudentRecord As String

    Function ValidateFields() As String
        Dim result As String = ""
        'first name field validation
        If txtFirstName.Text.Length = 0 Then
            result = result & "- First Name field is empty" & vbNewLine
        End If
        If InStr(txtFirstName.Text, " "c) = True Then
            result = result & "- First Name field has spaces" & vbNewLine
        End If
        If InStr(txtFirstName.Text, IsNumeric(txtFirstName.Text)) = True Then
            result = result & "- First Name field has numbers" & vbNewLine
        End If
        'last name field validation
        If txtLastName.Text.Length = 0 Then
            result = result & "- Last Name field is empty" & vbNewLine
        End If
        If InStr(txtLastName.Text, " "c) = True Then
            result = result & "- Last Name field has spaces" & vbNewLine
        End If
        If InStr(txtLastName.Text, IsNumeric(txtLastName.Text)) = True Then
            result = result & "- Last Name field has numbers" & vbNewLine
        End If
        'major validation
        If txtMajor.Text.Length = 0 Then
            result = result & "- Major field is empty" & vbNewLine
        End If
        If InStr(txtMajor.Text, " "c) = True Then
            result = result & "- Major field has spaces" & vbNewLine
        End If
        If IsNumeric(txtMajor.Text.Substring(0, 1)) = True Then
            result = result & "- Major field course code is incorrect" & vbNewLine
        End If
        'phone validation
        If txtPhone.Text.Length = 0 Then
            result = result & "- Phone field is empty" & vbNewLine
        End If
        If IsNumeric(txtPhone.Text) = False Then
            result = result & "- Phone field contains illegal characters" & vbNewLine
        End If
        'email validation
        If txtEmail.Text.Length = 0 Then
            result = result & "- Email field is empty" & vbNewLine
        End If
        If InStr(txtEmail.Text, "@") = False And InStr(txtEmail.Text, ".") Then
            result = result & "- Email field is invalid" & vbNewLine
        End If
        'GPA validation
        If txtGPA.Text.Length = 0 Then
            result = result & "- GPA field is empty" & vbNewLine
        End If
        If IsNumeric(txtGPA.Text) = False Then
            result = result & "- GPA field is invalid" & vbNewLine
        End If
        If CDbl(txtGPA.Text) < 0 Or CDbl(txtGPA.Text) > 4.0 Then
            result = result & "- GPA is out of bounds" & vbNewLine
        End If
        'return
        Return result
    End Function

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = DialogResult.Cancel
    End Sub

    Private Sub btnAddStudent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddStudent.Click
        Dim errorStr As String = ""
        errorStr = ValidateFields()
        If errorStr = "" Then
            'place all the student field data into the array of elements
            aryData(0) = txtFirstName.Text
            aryData(1) = txtLastName.Text
            aryData(2) = txtMajor.Text
            aryData(3) = txtPhone.Text
            aryData(4) = txtEmail.Text
            aryData(5) = txtGPA.Text

            'join the array elements and place the result into a string variable
            newStudentRecord = Join(aryData, " ")

            'create a link between the form and the file with streamwriter and write to the text file
            If System.IO.File.Exists(frmMain.FILE_NAME) = True Then
                Dim objWriter As New System.IO.StreamWriter(frmMain.FILE_NAME, True)
                objWriter.WriteLine(newStudentRecord)
                objWriter.Close()
            End If
            Me.DialogResult = DialogResult.OK
            Exit Sub
        Else
            MsgBox("The following errors were found in your input:" & vbNewLine & errorStr)
        End If
    End Sub
End Class

This post has been edited by ravencode: 07 December 2010 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

#6 bflosabre91  Icon User is offline

  • go sabres

Reputation: 105
  • View blog
  • Posts: 1,439
  • Joined: 22-February 08

Re: How to validate a form with multiple text-boxes...

Posted 07 December 2010 - 11:55 AM

i dont see any glaring issues there. If you put a breakpoint at the beginning of the sub and step through it all, does step through everything correctly?
Was This Post Helpful? 1
  • +
  • -

#7 ravencode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-December 10

Re: How to validate a form with multiple text-boxes...

Posted 07 December 2010 - 12:14 PM

View Postbflosabre91, on 07 December 2010 - 10:55 AM, said:

i dont see any glaring issues there. If you put a breakpoint at the beginning of the sub and step through it all, does step through everything correctly?


What do you mean by breakpoint?
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to validate a form with multiple text-boxes...

Posted 07 December 2010 - 12:22 PM

I just pasted your code into a project and made mistakes in each of the fields per your If Statements and received a messagebox listing all the problems.

Can you give me the data you entered for each one?

It could be something with the input as you are assuming a lot of conversions that Option Strict ON would not allow and these could be mis-interpreted by the compiler.

To see what I mean, place Option Strict On at the top of your code above the Class
Was This Post Helpful? 1
  • +
  • -

#9 ravencode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-December 10

Re: How to validate a form with multiple text-boxes...

Posted 07 December 2010 - 12:36 PM

View PostCharlieMay, on 07 December 2010 - 11:22 AM, said:

I just pasted your code into a project and made mistakes in each of the fields per your If Statements and received a messagebox listing all the problems.

Can you give me the data you entered for each one?

It could be something with the input as you are assuming a lot of conversions that Option Strict ON would not allow and these could be mis-interpreted by the compiler.

To see what I mean, place Option Strict On at the top of your code above the Class


Firstly try not inputting anything at all. The major field validation with Substring() is messed up. Major field must a structure of LetterNumberNumberNumber. After that try the following sequence of input:
-empty-
ja mes
t134 <-- This input is corrent because of the broken validation so we don't input any wrong data here
1800567-7345
alex@liveca
5.2

It skips the fact that Last Name has spaces in it and entrely ignored Email field issues.

This post has been edited by ravencode: 07 December 2010 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#10 Shadar  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: How to validate a form with multiple text-boxes...

Posted 07 December 2010 - 12:49 PM

I would use an errorprovider.

http://msdn.microsof...y/f6xht7x2.aspx
Was This Post Helpful? 1
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to validate a form with multiple text-boxes...

Posted 07 December 2010 - 12:56 PM

This is one of the problems I was talking about. You will need to first test to ensure that txtMajor.TextLength is greater than 0 as SubString will not work against an empty string.

So
If txtMajor.TextLength > 0 then
  if IsNumeric(txtMajor.Text.Substring(0,1)) = True Then
   ...
  end if
else
  ... Major field is empty
end if


This way you've accomplished both of those in one block.

Here is what I received when entering your dataAttached Image

You could use this same thing for anything where you're testing length of the string.

Also for:
        If InStr(txtEmail.Text, "@") = False And InStr(txtEmail.Text, ".") Then
            result = result & "- Email field is invalid" & vbNewLine
        End If


Try
If InStr(txtEmail.Text, "@") = False OR InStr(txtEmail.Text, ".") = False Then
            result = result & "- Email field is invalid" & vbNewLine
        End If

This post has been edited by CharlieMay: 07 December 2010 - 12:54 PM

Was This Post Helpful? 1
  • +
  • -

#12 ravencode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-December 10

Re: How to validate a form with multiple text-boxes...

Posted 07 December 2010 - 01:13 PM

View PostCharlieMay, on 07 December 2010 - 11:56 AM, said:

This is one of the problems I was talking about. You will need to first test to ensure that txtMajor.TextLength is greater than 0 as SubString will not work against an empty string.

So
If txtMajor.TextLength > 0 then
  if IsNumeric(txtMajor.Text.Substring(0,1)) = True Then
   ...
  end if
else
  ... Major field is empty
end if


This way you've accomplished both of those in one block.

Here is what I received when entering your dataAttachment raventest.JPG

You could use this same thing for anything where you're testing length of the string.

Also for:
        If InStr(txtEmail.Text, "@") = False And InStr(txtEmail.Text, ".") Then
            result = result & "- Email field is invalid" & vbNewLine
        End If


Try
If InStr(txtEmail.Text, "@") = False OR InStr(txtEmail.Text, ".") = False Then
            result = result & "- Email field is invalid" & vbNewLine
        End If


Changed the firstname field validation to this:

        If txtFirstName.Text.Length > 0 Then
            If InStr(txtFirstName.Text, " "c) = True Or IsNumeric(txtMajor.Text.Substring(0, 1)) = True Or InStr(txtFirstName.Text, " "c) = True Then
                result = result & "- First Name field is empty or contains illegal characters" & vbNewLine
            End If
        Else
            MsgBox("First Name is empty")
        End If

It still won't see the spaces in it.
Was This Post Helpful? 0
  • +
  • -

#13 Shadar  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: How to validate a form with multiple text-boxes...

Posted 07 December 2010 - 01:36 PM

Use the contains function for each textbox like the code below. You can put the validation code in each of the textbox validation sub OR you can put it all in the form validation sub.


Public Class Form4

    Private Sub TextFirstName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextFirstName.Validating

        If TextFirstName.Text.Contains(" ") Or TextFirstName.Text.Length = 0 Then
            errorprovider1.SetError(TextFirstName, "Name Cannot Contain Spaces and cannot be Zero length")
           e.Cancel = True
        Else
            errorprovider1.SetError(TextFirstName, "")
        End If

    End Sub

    Private Sub Form4_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        errorprovider1.BlinkStyle = ErrorBlinkStyle.NeverBlink
    End Sub
End Class


This post has been edited by Shadar: 07 December 2010 - 01:37 PM

Was This Post Helpful? 1
  • +
  • -

#14 Shadar  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 157
  • Joined: 26-May 09

Re: How to validate a form with multiple text-boxes...

Posted 07 December 2010 - 01:56 PM

and for the course: 3chars + numbers. Use this for the characters, you can use Isnumeric after you check the length and peel off the right 2 characters.

If Not System.Text.RegularExpressions.Regex.IsMatch(Strings.Left(TextFirstName.Text, 3), "[a-z|A-Z]+$") Then
            ep1.SetError(Textcourse, "First 3 Characters must be Alphanumeric")
        Else
            ep1.SetError(Textcourse, "")
        End If


This post has been edited by Shadar: 07 December 2010 - 01:57 PM

Was This Post Helpful? 1
  • +
  • -

#15 ravencode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 01-December 10

Re: How to validate a form with multiple text-boxes...

Posted 08 December 2010 - 08:02 AM

View PostShadar, on 07 December 2010 - 12:56 PM, said:

and for the course: 3chars + numbers. Use this for the characters, you can use Isnumeric after you check the length and peel off the right 2 characters.

If Not System.Text.RegularExpressions.Regex.IsMatch(Strings.Left(TextFirstName.Text, 3), "[a-z|A-Z]+$") Then
            ep1.SetError(Textcourse, "First 3 Characters must be Alphanumeric")
        Else
            ep1.SetError(Textcourse, "")
        End If


What would be the If for checking if First Name has numbers in it?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2