Validation of Checkbox and Dropdownlist

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to use the following script to see determine whether a
dropdownlist has a value in it or to see whether a checkbox has been checked.
I don't want the user to be able to do both. In other words I don't want a
user to check a checkbox and choose an item from a drop-down list box. I
want them to do one action or the other.
Here is my script to try and do this but im not sure if my logic is correct
as I get an error no matter what I do. Can someone please help me.

<script language=javascript>
function ValidateDropDownOrCheckBox(sender, args)
{
if ( document.WebForm2.DropDownList1.value ||&&
document.WebForm2.CheckBox1.checked )
{
args.IsValid = true;
return;
}
else
args.IsValid = false;
}
</script>

This is how I refer to this script
<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckBox">*</asp:CustomValidator>

Thanks for any help anyone can give me
 
You'll want to replace your ValidateDropDownOrCheckBox() function with
something like the following:

var ddl = document.WebForm2.DropDownList1;
var cb = document.WebForm2.CheckBox1;
args.IsValid = ((ddl.selectedIndex>0 && !cb.checked) ||
(ddl.selectedIndex==0 && cb.checked));

The first line sets the variable "ddl" to your drop-down-list.
The second line sets the variable "cb" to your checkbox.
The third line sets args.IsValid to true when (1) the ddl has an item
selected and the checkbox is not checked, or (2) the ddl has the 0th item
selected and the checkbox checked.

Hope that helps.
 
this worked great however I need to also add something in so as checkbox2 is
incorporated. Its ok for checkbox1 &2 to be ticked as long as Dropdownlist
box is not filled in and its ok for checkbox2 to be ticked providing
dropdownlist box is not filled again. Can you help me re-write the code to
include this please.
 
Back
Top