Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
A Web Form has 2 radio buttons (of the same group) & a ListBox. When the page loads for the 1st time, the 1st radio button is checked by default & the ListBox remains invisible. Only if the 2nd radio button is selected by a user, the ListBox becomes visible. I am hiding & unhiding the ListBox using JavaScript. Now I want to validate that if the 2nd radio button is selected, then at least one of the options in the ListBox should be selected by the user else show him an error message. How do I do this?

Thanks,

Arpan
 
Arpan: I tried the "Required Field Validator" but I guess it doesn't recognize listboxes. Anyway, you could probably stick strickly with Java by attaching an attribute to the listbox, etc etc.. However, if you can tolerate a post back you could accomplish this by using a PostBack from radiobutton No. 2, e.g.,

In the code behind:

Sub chkB_CheckedChanged(Sender As Object, e As EventArgs)
If lstA.SelectedIndex = -1 Then 'no selection
lblreq.Text="You must make a listbox selection!"
Exit Sub
Else
Response.Redirect("mypage.aspx")
Exit Sub
End If
End Sub

and in the HTML you'd have...

<HTML>
<body>
<form id=&quot;Form1&quot; runat=&quot;server&quot;>
<asp:Listbox id=&quot;lstA&quot; runat=&quot;server&quot; ..vis/invis Java here/><br>
<asp:radiobutton id=&quot;A&quot; runat=&quot;server&quot; GroupName=&quot;mygrp&quot; Text=&quot;First Choice&quot; Checked=&quot;True&quot;/>
<asp:radiobutton id=&quot;B&quot; runat=&quot;server&quot; GroupName=&quot;mygrp&quot; Text=&quot;Second Choice&quot; AutoPostBack=&quot;True&quot; OnCheckedChanged=&quot;chkB_CheckedChanged&quot;/>
<br><asp:Label id=&quot;lblreq&quot; runat=&quot;server&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;Red&quot;/>
</form>
</body>

...and you could toggle the visibility of the label in the code behind if necc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top