473,508 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c# Validation logic help required please

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
7 2159
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
13118
by: Earl Teigrob | last post by:
I wanted my "Terms and Conditions" Checkbox control to participate in my ASP.NET validation just like all the the other controls on the page. After some time of searching the web for an example of...
3
2181
by: Lisa Calla | last post by:
Hi, I've been struggling with this for a few days. I've seen bits and pieces (how to set up custom validation), but I can't seem to understand how to get client side validation for my custom...
5
1912
by: Sun Jian | last post by:
Hi, I am trying to customize the asp.net validation to achieve the following: Upon submitting the form, client side validation will run, and it will stop at the first error detected. For...
5
2914
by: Mattyw | last post by:
Hi, I'm relatively new to Web Forms, I have been using Required Field Validators and Regular Expression Validators on a Web Form I am developing and everything works as expected using Visual...
2
2342
by: daniel.boorn | last post by:
Form validation using JavaScript has never been as easy and simple! We have developed a free generic form validation script that can validate any form with very little JavaScript required in form!...
10
5685
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the...
7
3581
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
15
1850
by: MKO | last post by:
Hi guys I'm having a little trouble with form validation, I can get the entire form to validate without problems (i.e. when all fields set to required & valid data is entered) but can't get it to...
6
4504
by: gavy7210 | last post by:
hello i am using struts 1.2,Eclipse Platform Version: 3.4.2,mySql 5.0.1,jdk 1.5.. i have a login form(jsp) in which a user logs in,in case he doesnt enter his username and/or password an error...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.