TextBox Validation

DimMeAsLost

Active member
Joined
Jan 31, 2010
Messages
29
Programming Experience
1-3
I do not want to jump in someone's post, but if it is not acceptable, please delete. I apologize beforehand.

My question is the last code tag section above where the parameters are set for the input. If on the form there is a textbox that is required (on the db side to not allow nulls) how would you perform the validation before you tried to execute the update sql?

Ex: say 'SerialNumberTextbox.Text' was empty or if you needed to have character constraints on what was typed like varchar(15) and they have 16 or more characters, what would be a great practice to implement?

Thanks
 
TextBox validation has nothing specifically to do with SQL parameters. The reason you want to validate a TextBox isn't relevant to how you do it. What you do depends on what you're trying to achieve. If you want to limit the length of the data then the TextBox has a property for that. If you want to make sure the Text is not an empty string then that's exactly what you do. If you do need to test the Text then where you do it is also application-dependent. You might do it in the TextBox's own Validating or TextChanged event, or on the Click of a Button or somewhere else.
 
I understand the points you are making, but with my VBA background, I am looking at how its done in VB.NET. In VBA I would assign a value to the TAG property and then loop thru each control to find that tag and then see if is null using NZ() method.

I am looking for examples, but yet I do not want to pickup any bad examples or bad practices.


Thanks,
 
Without knowing how you want your app to behave I can't say more than I already have. As I said, when you validate depends on when you want to know whether the data is invalid. If you want to validate controls as they're populated then you'd handle their Validating events. If you don't care until the user clicks a Button then you'd handle the Click event of the Button and validate every control then. If you want something else then you'd do something else. This is an example of why design must come before development. If you don't know what your app is supposed to do then you can't write code to make it do it. You must decide on behaviour first.
 
Sorry, been away for a while. We moved our office to a new location and I was the facilitator of the move. New T3 connection is awesome!

anyway, back to your reply, I would like to validate textboxes to verify that they are not empty (blank)

I have a stored procedure with input parameters, and basically I need to verify that any required field in the database does not have a blank textbox before I run the update stored procedure.

I would like that when the user clicks on the 'update' button, if any required textbox (there may be some that are ok to be blank) is empty then a message box would tell them there is missing data in field "txtbox name here". Once they have corrected, then the update would start.

Thanks again for all of your help.
 
First place a marker, such as req in the Tag property of each textbox that requires an input, and use the following code:

VB.NET:
		For Each txt As TextBox In Me.Controls.OfType(Of TextBox)().Reverse
			If (String.IsNullOrEmpty(txt.Text)) AndAlso txt.Tag Is "req" Then
				txt.Focus()
				MessageBox.Show("Required entry in " & txt.Name)
				Exit For
			End If
		Next txt
 
Last edited:
Here's what I do:

VB.NET:
Private Function ValidateData() As Boolean

        Dim sMessage As String = ""
            If Me.cboRecipe.Text = "" Then
                sMessage &= "'Recipe'" & System.Environment.NewLine
            End If
            If Me.cboSieve.Text = "" Then
                sMessage &= "'Sieve'" & System.Environment.NewLine
            End If
            If Me.cboScalingWeight.Text = "" Then
                sMessage &= "'Scaling Weight'" & System.Environment.NewLine
            End If
            If Me.cboPieceType.Text = "" Then
                sMessage &= "'Piece Type'" & System.Environment.NewLine
            End If
            If Me.txtDCNumber.Text = "" Then
                sMessage &= "'DC Number'" & System.Environment.NewLine
            End If
            If Me.txtDescription.Text = "" Then
                sMessage &= "'Description'" & System.Environment.NewLine
            End If
            If Me.cboCoProducer.Text = "" Then
                sMessage &= "'Co-Producer'" & System.Environment.NewLine
            End If
       
        If sMessage = String.Empty Then
            ValidateData = True
        Else
            ValidateData = False
            MessageBox.Show("The following fields are missing information:" _
            & System.Environment.NewLine & sMessage & System.Environment.NewLine & "Please enter information into these fields and submit again", "Data Entry Error", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

    End Function

Then on the "update" button:

VB.NET:
If ValidateData() Then
[COLOR="green"]' do something interesting here[/COLOR]
End If

Basically, if any of those fields in the function are empty, the system flags them up in a messagebox to the user. It will only 'do something interesting here if all the fields have a value
 
Back
Top