PDA

View Full Version : Validation RadioButtonLists and CheckBoxLists


peachsoda03
07-22-2008, 11:03 PM
Hello all,

Just a quick forewarning... I am a complete newbie to asp.net. I code regularly in Coldfusion, but every now and then I my job will require me to use asp.net, and now is such a case. I'm finding that things I can do easily in CF are taking me hours in asp.net, such as this validation problem that I seem unable to solve.

I'm working on a simple web form that asks for basic information (name, address, etc). At the end of the form, it asks you to select one of two options on a RadioButtonList:

What are you moving?
<asp:RadioButtonList ID="whatMove" runat="server">
<asp:ListItem Value="wholeHouse"> Whole house: </asp:ListItem>
<asp:ListItem Value="fewRooms"> Just a few rooms: </asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="whatMove" ErrorMessage="Please select a move option."></asp:RequiredFieldValidator>

I've figured out the RequiredFieldValidator (which is surprisingly easy to use), which you can see above. However, depending on which option the user selects, I need to add additional validation.

If the user selects "Whole house" as an option, then they need to be forced to select an option from a drop down list:

<asp:DropDownList ID="whatMoveHouse" runat="server">
<asp:ListItem Value="-1">- Select an option -</asp:ListItem>
<asp:ListItem Value="1 bedroom house">1 bedroom house</asp:ListItem>
<asp:ListItem Value="2 bedroom house">2 bedroom house</asp:ListItem>
<asp:ListItem Value="3 bedroom house">3 bedroom house</asp:ListItem>
<asp:ListItem Value="4 bedroom house">4 bedroom house</asp:ListItem>
<asp:ListItem Value="5 bedroom house">5 bedroom house</asp:ListItem>
<asp:ListItem Value="6 bedroom house">6 bedroom house</asp:ListItem>
<asp:ListItem Value="Studio apartment">Studio apartment</asp:ListItem>
<asp:ListItem Value="1 bedroom apartment">1 bedroom apartment</asp:ListItem>
<asp:ListItem Value="2 bedroom apartment">2 bedroom apartment</asp:ListItem>
<asp:ListItem Value="3 bedroom apartment">3 bedroom apartment</asp:ListItem>
</asp:DropDownList>

If the user selected "Just a few rooms" from the list, then they need to be required to select at least one option in a CheckBoxList:

<asp:CheckBoxList ID="whatMoveRoom" runat="server" style="padding-left: 20px;">
<asp:ListItem Value="Kitchen"> Kitchen</asp:ListItem>
<asp:ListItem Value="Bedroom"> Bedroom</asp:ListItem>
<asp:ListItem Value="Family room"> Family room</asp:ListItem>
<asp:ListItem Value="Dining room"> Dining room</asp:ListItem>
<asp:ListItem Value="Bathroom"> Bathroom</asp:ListItem>
</asp:CheckBoxList>


From what I've read, using a CustomValidator is the way to go, which I've tried adding to the CheckBoxList:

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select at least one room type." ControlToValidate="whatMove"></asp:CustomValidator>

However, from this point on, I am lost. I have been messing with some for-loops and such but am unsure exactly what sort of checks I need to do. This is what I have so far:

Public Sub whatMoveRoomCheck(ByVal sender As Object, ByVal args As ServerValidateEventArgs)

For Each l As ListItem In whatMoveRoom.Items
If (l.Selected) Then
args.IsValid = True
Return
End If
Next

args.IsValid = False

End Sub


I am at my wit's end about this (it would have taken me a mere 10 minutes using html forms and javascript), and I've already put a good 6-7 hours into researching and testing things. No luck yet. Any help is greatly and very much appreciated. :)

Jennifer

Freon22
07-23-2008, 06:53 PM
If I was doing this I would put the DropDownList in one asp panel and the CheckBoxList in another panel. I would set both panels to Visible="false".

I would force a post back when they selected one of the RadioButtonList and would make the panel with the right control Visible="true". Since you don't need to validated the control that is not needed. I would more then likely also use Ajax so I would not have to do a full postback.

As far as the custom validation control I would call a javascript function to check if one of my CheckBoxList has been checked.

Like so.

<asp:CheckBoxList ID="whatMoveRoom" runat="server" style="padding-left: 20px;">
<asp:ListItem Value="Kitchen"> Kitchen</asp:ListItem>
<asp:ListItem Value="Bedroom"> Bedroom</asp:ListItem>
<asp:ListItem Value="Family room"> Family room</asp:ListItem>
<asp:ListItem Value="Dining room"> Dining room</asp:ListItem>
<asp:ListItem Value="Bathroom"> Bathroom</asp:ListItem>
</asp:CheckBoxList>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Please select at least one room type."
ClientValidationFunction="CheckBoxValidator">
</asp:CustomValidator>

<script language="javascript" type="text/javascript">
function CheckBoxValidator(sender, args)
{
var options = document.getElementById('whatMoveRoom').getElementsByTagName('input');
var ischecked=false;
args.IsValid =false;
for(i=0;i<options.length;i++)
{
var opt = options[i];
if(opt.type=="checkbox")
{
if(opt.checked)
{
ischecked= true;
args.IsValid = true;
}
}
}
}
</script>

Edit: I tested this in IE7 and Firefox works in both. btw I got this javascript code here http://www.dotnetspider.com/resources/2769-Validating-checkboxList-Asp-Net-control-using-jav.aspx