hello !
im new here and to VB. and i need some help on input validation for text boxes that are linked to radio buttons... at the moment im using If..Then, if i should do something different let me know..
i have 24 text boxes linked to 8 radio buttons and i want to validate certain text boxes per radio, how is it done?????

If rad1.Checked And txt1.Text.Length Or txt2.Text.Length Or txt3.Text.Length Or txt4.Text.Length Or txt5.Text.Length < 1 Then
            MessageBox.Show("Fill all boxes to read story", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        ElseIf rad2.Checked And txt1.Text.Length Or txt2.Text.Length Or txt3.Text.Length Or txt4.Text.Length Or txt5.Text.Length Or txt6.Text.Length Or txt7.Text.Length Or txt8.Text.Length Or txt9.Text.Length < 1 Then
            MessageBox.Show("Fill all boxes to read story", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        ElseIf rad3.Checked And txt1.Text.Length Or txt2.Text.Length Or txt3.Text.Length Or txt4.Text.Length Or txt5.Text.Length Or txt6.Text.Length Or txt7.Text.Length Or txt8.Text.Length Or txt9.Text.Length < 1 Then
            MessageBox.Show("Fill all boxes to read story", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        ElseIf rad4.Checked And txt1.Text.Length Or txt2.Text.Length Or txt3.Text.Length Or txt4.Text.Length Or txt5.Text.Length Or txt6.Text.Length Or txt7.Text.Length Or txt8.Text.Length Or txt9.Text.Length Or txt10.Text.Length Or txt11.Text.Length Or txt12.Text.Length Or txt13.Text.Length Or txt14.Text.Length Or txt15.Text.Length Or txt16.Text.Length Or txt17.Text.Length Or txt18.Text.Length Or txt19.Text.Length Or txt20.Text.Length Or txt21.Text.Length Or txt22.Text.Length Or txt23.Text.Length < 1 Then
            MessageBox.Show("Fill all boxes to read story", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        ElseIf rad5.Checked And txt1.Text.Length Or txt2.Text.Length Or txt3.Text.Length Or txt4.Text.Length Or txt5.Text.Length Or txt6.Text.Length Or txt7.Text.Length Or txt8.Text.Length Or txt9.Text.Length Or txt10.Text.Length Or txt11.Text.Length < 1 Then
            MessageBox.Show("Fill all boxes to read story", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        ElseIf rad6.Checked And txt1.Text.Length Or txt2.Text.Length Or txt3.Text.Length Or txt4.Text.Length Or txt5.Text.Length Or txt6.Text.Length Or txt7.Text.Length < 1 Then
            MessageBox.Show("Fill all boxes to read story", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        ElseIf rad7.Checked And txt1.Text.Length Or txt2.Text.Length Or txt3.Text.Length Or txt4.Text.Length Or txt5.Text.Length Or txt6.Text.Length Or txt7.Text.Length Or txt8.Text.Length Or txt9.Text.Length Or txt10.Text.Length Or txt11.Text.Length Or txt12.Text.Length Or txt13.Text.Length Or txt14.Text.Length Or txt15.Text.Length Or txt16.Text.Length Or txt17.Text.Length Or txt18.Text.Length Or txt19.Text.Length Or txt20.Text.Length Or txt21.Text.Length Or txt22.Text.Length Or txt23.Text.Length Or txt24.Text.Length < 1 Then
            MessageBox.Show("Fill all boxes to read story", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        ElseIf rad8.Checked And txt1.Text.Length Or txt2.Text.Length Or txt3.Text.Length Or txt4.Text.Length Or txt5.Text.Length Or txt6.Text.Length Or txt7.Text.Length Or txt8.Text.Length Or txt9.Text.Length Or txt10.Text.Length Or txt11.Text.Length Or txt12.Text.Length Or txt13.Text.Length Or txt14.Text.Length < 1 Then
            MessageBox.Show("Fill all boxes to read story", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Else

Dani AI

Generated

— the root problem is the long chained boolean expression: each textbox check must explicitly compare its value and the whole expression should be grouped. A much cleaner, maintainable approach is to map each radio button to the specific TextBox controls it requires, then validate that collection with one function. The snippet below shows a compact pattern using an ErrorProvider and LINQ to find missing fields.

' build the mapping once (e.g. Form_Load)
Dim requiredMap As New Dictionary(Of RadioButton, TextBox()) From {
    {rad1, New TextBox() {txt1, txt2, txt3}},
    {rad2, New TextBox() {txt1, txt2, txt3, txt4, txt5, txt6}}
    ' add other mappings...
}

Private ReadOnly validator As New ErrorProvider()

Private Function ValidateFields(fields As IEnumerable(Of TextBox)) As Boolean
    validator.Clear()
    Dim missing = fields.Where(Function(t) String.IsNullOrWhiteSpace(t.Text)).ToArray()
    If missing.Any() Then
        For Each tb In missing
            validator.SetError(tb, "Required")
            tb.BackColor = Color.LightPink
        Next
        missing(0).Focus()
        Return False
    End If
    Return True
End Function

' usage before processing:
Dim selected = requiredMap.Keys.FirstOrDefault(Function(r) r.Checked)
If selected IsNot Nothing AndAlso Not ValidateFields(requiredMap(selected)) Then Exit Sub

Use String.IsNullOrWhiteSpace for robust emptiness checks. As suggested, centralize the error display — but prefer a boolean validator (shown above) rather than repeating MessageBox code. As noted, helper functions are good; do not use exceptions for ordinary validation. Finally, avoid fragile If/Else chains: mapping + collection validation scales far better and is easier to maintain when you add or remove fields.

I would probably use a function to validate the textboxes, your current method doesn't work (you need to write txt1.Text.Length < 1 Or txt2.Text.Length < 1 Or txt3.Text.Length < 1 etc). It might also be easier to just do .Text.Length = 0

You could write a function to simplify the messagebox:

Private Sub ShowError()
        MessageBox.Show("Fill all boxes to read story", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    End Sub

hi

u can try this also .make a function which validate the textbox should not empty and check the condition either radio btn checked.

'Declaring Function
 Public Sub empty(ByVal txt As String, ByVal y As String)
        If txt = "" Then
            Throw New Exception(y & "fields could not be empty")
        End If
    End Sub
'Calling function
 empty(cmbbatchno.Text, " Batch No")

i hope it will help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.