By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

c# Validation logic help required please

P: n/a
I have some code which I call from a custom validator however I seem to have
got the logic wrong and im having trouble figuring out how to write my code
to get things to work the way I require. Below is the script I currently use
and what it does along with what I would like it to do. Can someone please
help me work how I can fix this.

<script>
function ValidateDropDownOrCheckBox(sender, args)
{
if ( document.forms[0].DropDownList1.value ||
( document.forms[0].CheckBox2.checked &&
document.forms[0].CheckBox1.checked )
)
{
args.IsValid = true;
return;
}
args.IsValid = false;
}
</script>

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckB ox">*</asp:CustomValidator>
MY PROBLEMS WITH THIS CODE ARE: -
At present the page is redirected if both checkboxes are checked and
something is selected in the dropdown list. This needs to show an error
Also the page shows an error if ONLY one checkbox is checked AND nothing is
selected in the dropdown list. I want this sequence to be allowed.
It also allows the page to redirect if one checkbox is checked and something
is selected in the dropdown list. This need to show an error.

I NEED MY LOGIC TO WORK LIKE SO: -
If I tick one of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If I tick both of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If neither checkbox is checked but something is selected in the dropdownlist
then I want the arg to be TRUE (in other words allow page event to run)
If niether checkbox is checked and nothing is selected in the dropdownlist
then i want the arg to be FALSE (in other words SHOW ERROR MESSAGE)
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi Stephen,
Try this:
bool valid=
(drop.value!=string.Empty && !(chk1.checked || chk2.checked))
||
(drop.value==string.Empty && (chk1.checked || chk2.checked));

The rule is: Valid if a value has been selected and no checkboxes have been
checked, or if no value has been selected and one or both checkboxes has
been checked.

Is this correct?

/Hugo

On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:
I have some code which I call from a custom validator however I seem to have
got the logic wrong and im having trouble figuring out how to write my code
to get things to work the way I require. Below is the script I currently use
and what it does along with what I would like it to do. Can someone please
help me work how I can fix this.

<script>
function ValidateDropDownOrCheckBox(sender, args)
{
if ( document.forms[0].DropDownList1.value ||
( document.forms[0].CheckBox2.checked &&
document.forms[0].CheckBox1.checked )
)
{
args.IsValid = true;
return;
}
args.IsValid = false;
}
</script>

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckB ox">*</asp:CustomValidator>
MY PROBLEMS WITH THIS CODE ARE: -
At present the page is redirected if both checkboxes are checked and
something is selected in the dropdown list. This needs to show an error
Also the page shows an error if ONLY one checkbox is checked AND nothing is
selected in the dropdown list. I want this sequence to be allowed.
It also allows the page to redirect if one checkbox is checked and something
is selected in the dropdown list. This need to show an error.

I NEED MY LOGIC TO WORK LIKE SO: -
If I tick one of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If I tick both of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If neither checkbox is checked but something is selected in the dropdownlist
then I want the arg to be TRUE (in other words allow page event to run)
If niether checkbox is checked and nothing is selected in the dropdownlist
then i want the arg to be FALSE (in other words SHOW ERROR MESSAGE)

Nov 16 '05 #2

P: n/a
thats great the logic is perfec, but im not totally sure how to write what
you just did into a javascript funtion or a funtion for the code behind page.
Do i need to declare bool, do I write this in the javascript. Could you
please help me use your code to re-write my javascript or but it into a
function in the code behind page.
Thanks so much for your help

"Hugo Wetterberg" wrote:
Hi Stephen,
Try this:
bool valid=
(drop.value!=string.Empty && !(chk1.checked || chk2.checked))
||
(drop.value==string.Empty && (chk1.checked || chk2.checked));

The rule is: Valid if a value has been selected and no checkboxes have been
checked, or if no value has been selected and one or both checkboxes has
been checked.

Is this correct?

/Hugo

On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:
I have some code which I call from a custom validator however I seem to have
got the logic wrong and im having trouble figuring out how to write my code
to get things to work the way I require. Below is the script I currently use
and what it does along with what I would like it to do. Can someone please
help me work how I can fix this.

<script>
function ValidateDropDownOrCheckBox(sender, args)
{
if ( document.forms[0].DropDownList1.value ||
( document.forms[0].CheckBox2.checked &&
document.forms[0].CheckBox1.checked )
)
{
args.IsValid = true;
return;
}
args.IsValid = false;
}
</script>

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckB ox">*</asp:CustomValidator>
MY PROBLEMS WITH THIS CODE ARE: -
At present the page is redirected if both checkboxes are checked and
something is selected in the dropdown list. This needs to show an error
Also the page shows an error if ONLY one checkbox is checked AND nothing is
selected in the dropdown list. I want this sequence to be allowed.
It also allows the page to redirect if one checkbox is checked and something
is selected in the dropdown list. This need to show an error.

I NEED MY LOGIC TO WORK LIKE SO: -
If I tick one of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If I tick both of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If neither checkbox is checked but something is selected in the dropdownlist
then I want the arg to be TRUE (in other words allow page event to run)
If niether checkbox is checked and nothing is selected in the dropdownlist
then i want the arg to be FALSE (in other words SHOW ERROR MESSAGE)

Nov 16 '05 #3

P: n/a
Well it's a litte problematic to validate multiple controls in ASP.Net 1.1,
but I belive that MS adresses this in ASP.Net 2. That means that it's
difficult to use a standard CustomValidator.

For the aspx-page:
<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id=drop runat="server">
<asp:ListItem Selected="True">Select a item</asp:ListItem>
<asp:ListItem Value="Hello">Hello</asp:ListItem>
<asp:ListItem Value="Hi">Hi</asp:ListItem>
</asp:DropDownList><BR>
<asp:CheckBox id=CheckBox1 runat="server"
Text="Chk1"></asp:CheckBox><BR>
<asp:CheckBox id=CheckBox2 runat="server"
Text="Chk1"></asp:CheckBox>
</P>
<P>
<asp:Button id=btnOk runat="server" Text="Ok"></asp:Button>
<asp:CustomValidator id=validator runat="server" ErrorMessage="Error"
ClientValidationFunction="validate"
ControlToValidate="drop">Error</asp:CustomValidator>
</P>
</form>

The codebehind would be like this if if you use a CustomValidator:
private void validator_ServerValidate(object source,
ServerValidateEventArgs args)
{
args.IsValid=
(drop.SelectedValue!=string.Empty &&
!(CheckBox1.Checked || CheckBox2.Checked))
||
(drop.SelectedValue==string.Empty &&
(CheckBox1.Checked || CheckBox2.Checked));
}

JScript client side validator function would be:
function validate(source, arguments)
{
var dropHasValue=document.getElementById("drop").value .length>0;
var checked1=document.getElementById("CheckBox1").chec ked;
var checked2=document.getElementById("CheckBox2").chec ked;

arguments.IsValid=
(dropHasValue && !(checked1 || checked2))
||
(!dropHasValue && (checked1 || checked2));
}

This will work, in a way. But the input will only be verified when the user
makes a selection, or non-selection, in the drop-down menu.

To validate you'll have to roll your own solution, but these snippets will
help you get started.

/Hugo
On Tue, 16 Nov 2004 03:12:01 -0800, Stephen wrote:
thats great the logic is perfec, but im not totally sure how to write what
you just did into a javascript funtion or a funtion for the code behind page.
Do i need to declare bool, do I write this in the javascript. Could you
please help me use your code to re-write my javascript or but it into a
function in the code behind page.
Thanks so much for your help

"Hugo Wetterberg" wrote:
Hi Stephen,
Try this:
bool valid=
(drop.value!=string.Empty && !(chk1.checked || chk2.checked))
||
(drop.value==string.Empty && (chk1.checked || chk2.checked));

The rule is: Valid if a value has been selected and no checkboxes have been
checked, or if no value has been selected and one or both checkboxes has
been checked.

Is this correct?

/Hugo

On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:
I have some code which I call from a custom validator however I seem to have
got the logic wrong and im having trouble figuring out how to write my code
to get things to work the way I require. Below is the script I currently use
and what it does along with what I would like it to do. Can someone please
help me work how I can fix this.

<script>
function ValidateDropDownOrCheckBox(sender, args)
{
if ( document.forms[0].DropDownList1.value ||
( document.forms[0].CheckBox2.checked &&
document.forms[0].CheckBox1.checked )
)
{
args.IsValid = true;
return;
}
args.IsValid = false;
}
</script>

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckB ox">*</asp:CustomValidator>
MY PROBLEMS WITH THIS CODE ARE: -
At present the page is redirected if both checkboxes are checked and
something is selected in the dropdown list. This needs to show an error
Also the page shows an error if ONLY one checkbox is checked AND nothing is
selected in the dropdown list. I want this sequence to be allowed.
It also allows the page to redirect if one checkbox is checked and something
is selected in the dropdown list. This need to show an error.

I NEED MY LOGIC TO WORK LIKE SO: -
If I tick one of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If I tick both of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If neither checkbox is checked but something is selected in the dropdownlist
then I want the arg to be TRUE (in other words allow page event to run)
If niether checkbox is checked and nothing is selected in the dropdownlist
then i want the arg to be FALSE (in other words SHOW ERROR MESSAGE)

Nov 16 '05 #4

P: n/a
Hi Stephen,
I made a mistake in the code, you must set the empty list items value to ""
to make the code work:
<asp:ListItem Selected="True" value="">Select a item</asp:ListItem>

/Hugo

On Tue, 16 Nov 2004 13:16:30 +0100, Hugo Wetterberg wrote:
Well it's a litte problematic to validate multiple controls in ASP.Net 1.1,
but I belive that MS adresses this in ASP.Net 2. That means that it's
difficult to use a standard CustomValidator.

For the aspx-page:
<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id=drop runat="server">
<asp:ListItem Selected="True">Select a item</asp:ListItem>
<asp:ListItem Value="Hello">Hello</asp:ListItem>
<asp:ListItem Value="Hi">Hi</asp:ListItem>
</asp:DropDownList><BR>
<asp:CheckBox id=CheckBox1 runat="server"
Text="Chk1"></asp:CheckBox><BR>
<asp:CheckBox id=CheckBox2 runat="server"
Text="Chk1"></asp:CheckBox>
</P>
<P>
<asp:Button id=btnOk runat="server" Text="Ok"></asp:Button>
<asp:CustomValidator id=validator runat="server" ErrorMessage="Error"
ClientValidationFunction="validate"
ControlToValidate="drop">Error</asp:CustomValidator>
</P>
</form>

The codebehind would be like this if if you use a CustomValidator:
private void validator_ServerValidate(object source,
ServerValidateEventArgs args)
{
args.IsValid=
(drop.SelectedValue!=string.Empty &&
!(CheckBox1.Checked || CheckBox2.Checked))
||
(drop.SelectedValue==string.Empty &&
(CheckBox1.Checked || CheckBox2.Checked));
}

JScript client side validator function would be:
function validate(source, arguments)
{
var dropHasValue=document.getElementById("drop").value .length>0;
var checked1=document.getElementById("CheckBox1").chec ked;
var checked2=document.getElementById("CheckBox2").chec ked;

arguments.IsValid=
(dropHasValue && !(checked1 || checked2))
||
(!dropHasValue && (checked1 || checked2));
}

This will work, in a way. But the input will only be verified when the user
makes a selection, or non-selection, in the drop-down menu.

To validate you'll have to roll your own solution, but these snippets will
help you get started.

/Hugo
On Tue, 16 Nov 2004 03:12:01 -0800, Stephen wrote:
thats great the logic is perfec, but im not totally sure how to write what
you just did into a javascript funtion or a funtion for the code behind page.
Do i need to declare bool, do I write this in the javascript. Could you
please help me use your code to re-write my javascript or but it into a
function in the code behind page.
Thanks so much for your help

"Hugo Wetterberg" wrote:
Hi Stephen,
Try this:
bool valid=
(drop.value!=string.Empty && !(chk1.checked || chk2.checked))
||
(drop.value==string.Empty && (chk1.checked || chk2.checked));

The rule is: Valid if a value has been selected and no checkboxes have been
checked, or if no value has been selected and one or both checkboxes has
been checked.

Is this correct?

/Hugo

On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:

I have some code which I call from a custom validator however I seem to have
got the logic wrong and im having trouble figuring out how to write my code
to get things to work the way I require. Below is the script I currently use
and what it does along with what I would like it to do. Can someone please
help me work how I can fix this.

<script>
function ValidateDropDownOrCheckBox(sender, args)
{
if ( document.forms[0].DropDownList1.value ||
( document.forms[0].CheckBox2.checked &&
document.forms[0].CheckBox1.checked )
)
{
args.IsValid = true;
return;
}
args.IsValid = false;
}
</script>

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckB ox">*</asp:CustomValidator>
MY PROBLEMS WITH THIS CODE ARE: -
At present the page is redirected if both checkboxes are checked and
something is selected in the dropdown list. This needs to show an error
Also the page shows an error if ONLY one checkbox is checked AND nothing is
selected in the dropdown list. I want this sequence to be allowed.
It also allows the page to redirect if one checkbox is checked and something
is selected in the dropdown list. This need to show an error.

I NEED MY LOGIC TO WORK LIKE SO: -
If I tick one of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If I tick both of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If neither checkbox is checked but something is selected in the dropdownlist
then I want the arg to be TRUE (in other words allow page event to run)
If niether checkbox is checked and nothing is selected in the dropdownlist
then i want the arg to be FALSE (in other words SHOW ERROR MESSAGE)

Nov 16 '05 #5

P: n/a
Stephen wrote:
I have some code which I call from a custom validator however I seem
to have got the logic wrong and im having trouble figuring out how to
write my code to get things to work the way I require. Below is the
script I currently use and what it does along with what I would like
it to do. Can someone please help me work how I can fix this.

<script>
function ValidateDropDownOrCheckBox(sender, args)
{
if ( document.forms[0].DropDownList1.value ||
( document.forms[0].CheckBox2.checked &&
document.forms[0].CheckBox1.checked )
)
{
args.IsValid = true;
return;
}
args.IsValid = false;
}
</script>

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckB ox">*</asp:CustomValidator>
MY PROBLEMS WITH THIS CODE ARE: -
At present the page is redirected if both checkboxes are checked and
something is selected in the dropdown list. This needs to show an
error Also the page shows an error if ONLY one checkbox is checked
AND nothing is selected in the dropdown list. I want this sequence to
be allowed.
It also allows the page to redirect if one checkbox is checked and
something is selected in the dropdown list. This need to show an
error.

I NEED MY LOGIC TO WORK LIKE SO: -
If I tick one of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page
event to run)
If I tick both of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page
event to run)
If neither checkbox is checked but something is selected in the
dropdownlist then I want the arg to be TRUE (in other words allow
page event to run)
If niether checkbox is checked and nothing is selected in the
dropdownlist then i want the arg to be FALSE (in other words SHOW
ERROR MESSAGE)


This has nothing to do with C#, it's purely client-side javascript. However:

Let's see:
If I understand correctly, you want to have either at least one of the
checkboxed checked, or a dropdown value selected. This means EITHER
this should be true:
cb1.checked || cb2.checked
OR this should be true
ddl.value != ''

Then the complete test could be:
(cb1.checked || cb2.checked) != (ddl.value != '')

Or, to use your code:

function ValidateDropDownOrCheckBox(sender, args)
{
args.IsValid = ( ( document.forms[0].CheckBox1.checked ||
document.forms[0].CheckBox2.checked ) !=
( document.forms[0].DropDownList1.value != '') );
}
Hans Kesting
Nov 16 '05 #6

P: n/a
I managed to get the following code to work ok. Do you see any drawbacks or
any problems with this. Would you no how to write this in a function in the
code-behind page as I don't think they want me to use client-side scripting.

<script language="javascript">
function ValidateDropDownOrCheckBox(sender, args)
{
var ddl = document.Form1.lstReps;
var cb = document.Form1.chkAreYouLitigant;
var cb1 = document.Form1.chkPersonalLitigant;
args.IsValid = ((ddl.selectedIndex>0 && !(cb.checked || cb1.checked)) ||
ddl.selectedIndex==0 && (cb.checked || cb1.checked));
/* The rule is: Valid if a value has been selected and no checkboxes have been
checked, or if no value has been selected and one or both checkboxes has
been checked. */

}
</script>

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="You must either select a representative OR choose a small claim
type. You cannot do both" ClientValidationFunction =
"ValidateDropDownOrCheckBox">*</asp:CustomValidator>

"Hugo Wetterberg" wrote:
Well it's a litte problematic to validate multiple controls in ASP.Net 1.1,
but I belive that MS adresses this in ASP.Net 2. That means that it's
difficult to use a standard CustomValidator.

For the aspx-page:
<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id=drop runat="server">
<asp:ListItem Selected="True">Select a item</asp:ListItem>
<asp:ListItem Value="Hello">Hello</asp:ListItem>
<asp:ListItem Value="Hi">Hi</asp:ListItem>
</asp:DropDownList><BR>
<asp:CheckBox id=CheckBox1 runat="server"
Text="Chk1"></asp:CheckBox><BR>
<asp:CheckBox id=CheckBox2 runat="server"
Text="Chk1"></asp:CheckBox>
</P>
<P>
<asp:Button id=btnOk runat="server" Text="Ok"></asp:Button>
<asp:CustomValidator id=validator runat="server" ErrorMessage="Error"
ClientValidationFunction="validate"
ControlToValidate="drop">Error</asp:CustomValidator>
</P>
</form>

The codebehind would be like this if if you use a CustomValidator:
private void validator_ServerValidate(object source,
ServerValidateEventArgs args)
{
args.IsValid=
(drop.SelectedValue!=string.Empty &&
!(CheckBox1.Checked || CheckBox2.Checked))
||
(drop.SelectedValue==string.Empty &&
(CheckBox1.Checked || CheckBox2.Checked));
}

JScript client side validator function would be:
function validate(source, arguments)
{
var dropHasValue=document.getElementById("drop").value .length>0;
var checked1=document.getElementById("CheckBox1").chec ked;
var checked2=document.getElementById("CheckBox2").chec ked;

arguments.IsValid=
(dropHasValue && !(checked1 || checked2))
||
(!dropHasValue && (checked1 || checked2));
}

This will work, in a way. But the input will only be verified when the user
makes a selection, or non-selection, in the drop-down menu.

To validate you'll have to roll your own solution, but these snippets will
help you get started.

/Hugo
On Tue, 16 Nov 2004 03:12:01 -0800, Stephen wrote:
thats great the logic is perfec, but im not totally sure how to write what
you just did into a javascript funtion or a funtion for the code behind page.
Do i need to declare bool, do I write this in the javascript. Could you
please help me use your code to re-write my javascript or but it into a
function in the code behind page.
Thanks so much for your help

"Hugo Wetterberg" wrote:
Hi Stephen,
Try this:
bool valid=
(drop.value!=string.Empty && !(chk1.checked || chk2.checked))
||
(drop.value==string.Empty && (chk1.checked || chk2.checked));

The rule is: Valid if a value has been selected and no checkboxes have been
checked, or if no value has been selected and one or both checkboxes has
been checked.

Is this correct?

/Hugo

On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:

I have some code which I call from a custom validator however I seem to have
got the logic wrong and im having trouble figuring out how to write my code
to get things to work the way I require. Below is the script I currently use
and what it does along with what I would like it to do. Can someone please
help me work how I can fix this.

<script>
function ValidateDropDownOrCheckBox(sender, args)
{
if ( document.forms[0].DropDownList1.value ||
( document.forms[0].CheckBox2.checked &&
document.forms[0].CheckBox1.checked )
)
{
args.IsValid = true;
return;
}
args.IsValid = false;
}
</script>

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckB ox">*</asp:CustomValidator>
MY PROBLEMS WITH THIS CODE ARE: -
At present the page is redirected if both checkboxes are checked and
something is selected in the dropdown list. This needs to show an error
Also the page shows an error if ONLY one checkbox is checked AND nothing is
selected in the dropdown list. I want this sequence to be allowed.
It also allows the page to redirect if one checkbox is checked and something
is selected in the dropdown list. This need to show an error.

I NEED MY LOGIC TO WORK LIKE SO: -
If I tick one of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If I tick both of the checkboxes (and nothing is selected in the
dropdownlist) I want the arg to be TRUE (in other words allow page event to
run)
If neither checkbox is checked but something is selected in the dropdownlist
then I want the arg to be TRUE (in other words allow page event to run)
If niether checkbox is checked and nothing is selected in the dropdownlist
then i want the arg to be FALSE (in other words SHOW ERROR MESSAGE)

Nov 16 '05 #7

P: n/a
Ok, here's the code for a sample page.

/Hugo

On Tue, 16 Nov 2004 05:29:03 -0800, Stephen wrote:
I managed to get the following code to work ok. Do you see any drawbacks or
any problems with this. Would you no how to write this in a function in the
code-behind page as I don't think they want me to use client-side scripting.

<script language="javascript">
function ValidateDropDownOrCheckBox(sender, args)
{
var ddl = document.Form1.lstReps;
var cb = document.Form1.chkAreYouLitigant;
var cb1 = document.Form1.chkPersonalLitigant;
args.IsValid = ((ddl.selectedIndex>0 && !(cb.checked || cb1.checked)) ||
ddl.selectedIndex==0 && (cb.checked || cb1.checked));
/* The rule is: Valid if a value has been selected and no checkboxes have been
checked, or if no value has been selected and one or both checkboxes has
been checked. */

}
</script>

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="You must either select a representative OR choose a small claim
type. You cannot do both" ClientValidationFunction =
"ValidateDropDownOrCheckBox">*</asp:CustomValidator>

"Hugo Wetterberg" wrote:
Well it's a litte problematic to validate multiple controls in ASP.Net 1.1,
but I belive that MS adresses this in ASP.Net 2. That means that it's
difficult to use a standard CustomValidator.

For the aspx-page:
<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id=drop runat="server">
<asp:ListItem Selected="True">Select a item</asp:ListItem>
<asp:ListItem Value="Hello">Hello</asp:ListItem>
<asp:ListItem Value="Hi">Hi</asp:ListItem>
</asp:DropDownList><BR>
<asp:CheckBox id=CheckBox1 runat="server"
Text="Chk1"></asp:CheckBox><BR>
<asp:CheckBox id=CheckBox2 runat="server"
Text="Chk1"></asp:CheckBox>
</P>
<P>
<asp:Button id=btnOk runat="server" Text="Ok"></asp:Button>
<asp:CustomValidator id=validator runat="server" ErrorMessage="Error"
ClientValidationFunction="validate"
ControlToValidate="drop">Error</asp:CustomValidator>
</P>
</form>

The codebehind would be like this if if you use a CustomValidator:
private void validator_ServerValidate(object source,
ServerValidateEventArgs args)
{
args.IsValid=
(drop.SelectedValue!=string.Empty &&
!(CheckBox1.Checked || CheckBox2.Checked))
||
(drop.SelectedValue==string.Empty &&
(CheckBox1.Checked || CheckBox2.Checked));
}

JScript client side validator function would be:
function validate(source, arguments)
{
var dropHasValue=document.getElementById("drop").value .length>0;
var checked1=document.getElementById("CheckBox1").chec ked;
var checked2=document.getElementById("CheckBox2").chec ked;

arguments.IsValid=
(dropHasValue && !(checked1 || checked2))
||
(!dropHasValue && (checked1 || checked2));
}

This will work, in a way. But the input will only be verified when the user
makes a selection, or non-selection, in the drop-down menu.

To validate you'll have to roll your own solution, but these snippets will
help you get started.

/Hugo
On Tue, 16 Nov 2004 03:12:01 -0800, Stephen wrote:
thats great the logic is perfec, but im not totally sure how to write what
you just did into a javascript funtion or a funtion for the code behind page.
Do i need to declare bool, do I write this in the javascript. Could you
please help me use your code to re-write my javascript or but it into a
function in the code behind page.
Thanks so much for your help

"Hugo Wetterberg" wrote:

Hi Stephen,
Try this:
bool valid=
(drop.value!=string.Empty && !(chk1.checked || chk2.checked))
||
(drop.value==string.Empty && (chk1.checked || chk2.checked));

The rule is: Valid if a value has been selected and no checkboxes have been
checked, or if no value has been selected and one or both checkboxes has
been checked.

Is this correct?

/Hugo

On Tue, 16 Nov 2004 02:09:01 -0800, Stephen wrote:

> I have some code which I call from a custom validator however I seem to have
> got the logic wrong and im having trouble figuring out how to write my code
> to get things to work the way I require. Below is the script I currently use
> and what it does along with what I would like it to do. Can someone please
> help me work how I can fix this.
>
> <script>
> function ValidateDropDownOrCheckBox(sender, args)
> {
> if ( document.forms[0].DropDownList1.value ||
> ( document.forms[0].CheckBox2.checked &&
> document.forms[0].CheckBox1.checked )
> )
> {
> args.IsValid = true;
> return;
> }
> args.IsValid = false;
> }
> </script>
>
> <asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
> ErrorMessage="Must enter either or"
> ClientValidationFunction="ValidateDropDownOrCheckB ox">*</asp:CustomValidator>
>
>
> MY PROBLEMS WITH THIS CODE ARE: -
> At present the page is redirected if both checkboxes are checked and
> something is selected in the dropdown list. This needs to show an error
> Also the page shows an error if ONLY one checkbox is checked AND nothing is
> selected in the dropdown list. I want this sequence to be allowed.
> It also allows the page to redirect if one checkbox is checked and something
> is selected in the dropdown list. This need to show an error.
>
> I NEED MY LOGIC TO WORK LIKE SO: -
> If I tick one of the checkboxes (and nothing is selected in the
> dropdownlist) I want the arg to be TRUE (in other words allow page event to
> run)
> If I tick both of the checkboxes (and nothing is selected in the
> dropdownlist) I want the arg to be TRUE (in other words allow page event to
> run)
> If neither checkbox is checked but something is selected in the dropdownlist
> then I want the arg to be TRUE (in other words allow page event to run)
> If niether checkbox is checked and nothing is selected in the dropdownlist
> then i want the arg to be FALSE (in other words SHOW ERROR MESSAGE)

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.