Validating user input. Either OR case

G

Guest

Could someone please help me with some validation. I have to write code which
checks to see whether a dropdown list is populated with a value or a checkbox
is checked. I want the code to run on the on-click of a button.
My page has 1 dropdown list and two checkboxes. So I either want the user
to choose an item from the dropdownlist OR tick a checkbox(it doesn't matter
if they tick both checkboxes). I can't allow the user to fill in the
dropdownlist and tick a checkbox.

Im pretty new to coding and im not sure what is the best way to do this. Can
I use a custom validator or would I be better putting it on the on-click
event of the button on the code behind page. I would also like the error
messages to appear in my Error Summary which is in a panel at the bottom of
my page.

This is the event ill probably need to place my code
private void Button1_Click(object sender, System.EventArgs e)
{
//Validate whether a item is selected in dropdown list OR a one/both
checkboxes is checked. Cant allow user to check a checkbox and choose an item
from the dropdown list
}

Could someone please help me write this code, im really not sure at all.

Here is the html code. for the page im using.

<table style="WIDTH: 305px; HEIGHT: 102px" height="102" width="305"
border="2">
<tr>
<td style="HEIGHT: 31px"><asp:dropdownlist id="DropDownList1" runat="server"
Width="231px">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Blue">Blue</asp:ListItem>
<asp:ListItem Value="Yellow">Yellow</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:dropdownlist></td>
</tr>
<tr>
<td style="HEIGHT: 31px"><asp:checkbox id="CheckBox2" runat="server"
Text="No Colour"></asp:checkbox>
<asp:checkbox id="CheckBox1" runat="server" Text="Any Colour"></asp:checkbox>
</td>
</tr>
<TR>
<TD><asp:button id="Button1" runat="server" Text="Button"></asp:button></TD>
</TR>
<tr><td>
<asp:panel id="pnMessagePanel" runat="server" Height="45px" Width="98.13%">
<asp:ValidationSummary id="vsSummaryOfErrors" runat="server" Width="897px"
Height="8px" BackColor="#ffffcc">
</asp:ValidationSummary>
</asp:panel>
</td></tr>
</table>

Thanks very much for any help anyone can give me
 
D

Dan Bass

Stephen,

One point of clarification, are you refering to an object being selected in
your dropdownlist which is not null?
Note that this is different to whether the dropdownlist is populated with
items...

If so, then client-side validation would be one way to go.

Write something like the following in the HTML <Head> tags

<script>

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

</script>


Then what you need to do is create a custom validator on your page (which
would be the error message you want to display), and reference our
javascript function by populating the "ClientValidationFunction" with
"ValidateDropDownOrCheckBox"

This will not let the user click on any post back button until the
args.IsValid is set to true in the javascript function.


Good luck.

Dan.
 
G

Guest

I tried the following below and the script kicks in however but no matter
what I do it won't let me move forward even if I select an item in the
dropdown list or check checkbox2. The error message keeps appearing.

One point of clarification, are you refering to an object being selected in
your dropdownlist which is not null?
Just want to check wether something has been selected. Yes then something
which is not null. I need to change the script slightly as well so as both
checkbox1 and checkbox2 are included. If checkbox1 &2 are checked that is ok
as long as nothing is chosen from the dropdown list. and if an item is chosen
from the dropdownlist then neither checkbox can be checked.

Can you provide me with a little more help with this. I really appreciate
your help.

Code im using is as follows: -

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

Onclick event of the button is: -
protected void Button2_Click(object sender, System.EventArgs e)
{
Response.Redirect("WebForm2.aspx");
}

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckBox">
*</asp:CustomValidator>


Dan Bass said:
Stephen,

One point of clarification, are you refering to an object being selected in
your dropdownlist which is not null?
Note that this is different to whether the dropdownlist is populated with
items...

If so, then client-side validation would be one way to go.

Write something like the following in the HTML <Head> tags

<script>

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

</script>


Then what you need to do is create a custom validator on your page (which
would be the error message you want to display), and reference our
javascript function by populating the "ClientValidationFunction" with
"ValidateDropDownOrCheckBox"

This will not let the user click on any post back button until the
args.IsValid is set to true in the javascript function.


Good luck.

Dan.


Stephen said:
Could someone please help me with some validation. I have to write code
which
checks to see whether a dropdown list is populated with a value or a
checkbox
is checked. I want the code to run on the on-click of a button.
My page has 1 dropdown list and two checkboxes. So I either want the user
to choose an item from the dropdownlist OR tick a checkbox(it doesn't
matter
if they tick both checkboxes). I can't allow the user to fill in the
dropdownlist and tick a checkbox.

Im pretty new to coding and im not sure what is the best way to do this.
Can
I use a custom validator or would I be better putting it on the on-click
event of the button on the code behind page. I would also like the error
messages to appear in my Error Summary which is in a panel at the bottom
of
my page.

This is the event ill probably need to place my code
private void Button1_Click(object sender, System.EventArgs e)
{
//Validate whether a item is selected in dropdown list OR a one/both
checkboxes is checked. Cant allow user to check a checkbox and choose an
item
from the dropdown list
}

Could someone please help me write this code, im really not sure at all.

Here is the html code. for the page im using.

<table style="WIDTH: 305px; HEIGHT: 102px" height="102" width="305"
border="2">
<tr>
<td style="HEIGHT: 31px"><asp:dropdownlist id="DropDownList1"
runat="server"
Width="231px">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Blue">Blue</asp:ListItem>
<asp:ListItem Value="Yellow">Yellow</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:dropdownlist></td>
</tr>
<tr>
<td style="HEIGHT: 31px"><asp:checkbox id="CheckBox2" runat="server"
Text="No Colour"></asp:checkbox>
<asp:checkbox id="CheckBox1" runat="server" Text="Any
Colour"></asp:checkbox>
</td>
</tr>
<TR>
<TD><asp:button id="Button1" runat="server"
Text="Button"></asp:button></TD>
</TR>
<tr><td>
<asp:panel id="pnMessagePanel" runat="server" Height="45px"
Width="98.13%">
<asp:ValidationSummary id="vsSummaryOfErrors" runat="server" Width="897px"
Height="8px" BackColor="#ffffcc">
</asp:ValidationSummary>
</asp:panel>
</td></tr>
</table>

Thanks very much for any help anyone can give me
 
D

Dan Bass

Stephen

you weren't too far off...

Try this:


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>



Stephen said:
I tried the following below and the script kicks in however but no matter
what I do it won't let me move forward even if I select an item in the
dropdown list or check checkbox2. The error message keeps appearing.

One point of clarification, are you refering to an object being selected
in
your dropdownlist which is not null?
Just want to check wether something has been selected. Yes then something
which is not null. I need to change the script slightly as well so as
both
checkbox1 and checkbox2 are included. If checkbox1 &2 are checked that is
ok
as long as nothing is chosen from the dropdown list. and if an item is
chosen
from the dropdownlist then neither checkbox can be checked.

Can you provide me with a little more help with this. I really appreciate
your help.

Code im using is as follows: -

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

Onclick event of the button is: -
protected void Button2_Click(object sender, System.EventArgs e)
{
Response.Redirect("WebForm2.aspx");
}

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckBox">
*</asp:CustomValidator>


Dan Bass said:
Stephen,

One point of clarification, are you refering to an object being selected
in
your dropdownlist which is not null?
Note that this is different to whether the dropdownlist is populated with
items...

If so, then client-side validation would be one way to go.

Write something like the following in the HTML <Head> tags

<script>

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

</script>


Then what you need to do is create a custom validator on your page (which
would be the error message you want to display), and reference our
javascript function by populating the "ClientValidationFunction" with
"ValidateDropDownOrCheckBox"

This will not let the user click on any post back button until the
args.IsValid is set to true in the javascript function.


Good luck.

Dan.


Stephen said:
Could someone please help me with some validation. I have to write code
which
checks to see whether a dropdown list is populated with a value or a
checkbox
is checked. I want the code to run on the on-click of a button.
My page has 1 dropdown list and two checkboxes. So I either want the
user
to choose an item from the dropdownlist OR tick a checkbox(it doesn't
matter
if they tick both checkboxes). I can't allow the user to fill in the
dropdownlist and tick a checkbox.

Im pretty new to coding and im not sure what is the best way to do
this.
Can
I use a custom validator or would I be better putting it on the
on-click
event of the button on the code behind page. I would also like the
error
messages to appear in my Error Summary which is in a panel at the
bottom
of
my page.

This is the event ill probably need to place my code
private void Button1_Click(object sender, System.EventArgs e)
{
//Validate whether a item is selected in dropdown list OR a one/both
checkboxes is checked. Cant allow user to check a checkbox and choose
an
item
from the dropdown list
}

Could someone please help me write this code, im really not sure at
all.

Here is the html code. for the page im using.

<table style="WIDTH: 305px; HEIGHT: 102px" height="102" width="305"
border="2">
<tr>
<td style="HEIGHT: 31px"><asp:dropdownlist id="DropDownList1"
runat="server"
Width="231px">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Blue">Blue</asp:ListItem>
<asp:ListItem Value="Yellow">Yellow</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:dropdownlist></td>
</tr>
<tr>
<td style="HEIGHT: 31px"><asp:checkbox id="CheckBox2" runat="server"
Text="No Colour"></asp:checkbox>
<asp:checkbox id="CheckBox1" runat="server" Text="Any
Colour"></asp:checkbox>
</td>
</tr>
<TR>
<TD><asp:button id="Button1" runat="server"
Text="Button"></asp:button></TD>
</TR>
<tr><td>
<asp:panel id="pnMessagePanel" runat="server" Height="45px"
Width="98.13%">
<asp:ValidationSummary id="vsSummaryOfErrors" runat="server"
Width="897px"
Height="8px" BackColor="#ffffcc">
</asp:ValidationSummary>
</asp:panel>
</td></tr>
</table>

Thanks very much for any help anyone can give me
 
G

Guest

Sorry but this still doesn't seem to work.
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)

Dan Bass said:
Stephen

you weren't too far off...

Try this:


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>



Stephen said:
I tried the following below and the script kicks in however but no matter
what I do it won't let me move forward even if I select an item in the
dropdown list or check checkbox2. The error message keeps appearing.

One point of clarification, are you refering to an object being selected
in
your dropdownlist which is not null?
Just want to check wether something has been selected. Yes then something
which is not null. I need to change the script slightly as well so as
both
checkbox1 and checkbox2 are included. If checkbox1 &2 are checked that is
ok
as long as nothing is chosen from the dropdown list. and if an item is
chosen
from the dropdownlist then neither checkbox can be checked.

Can you provide me with a little more help with this. I really appreciate
your help.

Code im using is as follows: -

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

Onclick event of the button is: -
protected void Button2_Click(object sender, System.EventArgs e)
{
Response.Redirect("WebForm2.aspx");
}

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckBox">
*</asp:CustomValidator>


Dan Bass said:
Stephen,

One point of clarification, are you refering to an object being selected
in
your dropdownlist which is not null?
Note that this is different to whether the dropdownlist is populated with
items...

If so, then client-side validation would be one way to go.

Write something like the following in the HTML <Head> tags

<script>

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

</script>


Then what you need to do is create a custom validator on your page (which
would be the error message you want to display), and reference our
javascript function by populating the "ClientValidationFunction" with
"ValidateDropDownOrCheckBox"

This will not let the user click on any post back button until the
args.IsValid is set to true in the javascript function.


Good luck.

Dan.


Could someone please help me with some validation. I have to write code
which
checks to see whether a dropdown list is populated with a value or a
checkbox
is checked. I want the code to run on the on-click of a button.
My page has 1 dropdown list and two checkboxes. So I either want the
user
to choose an item from the dropdownlist OR tick a checkbox(it doesn't
matter
if they tick both checkboxes). I can't allow the user to fill in the
dropdownlist and tick a checkbox.

Im pretty new to coding and im not sure what is the best way to do
this.
Can
I use a custom validator or would I be better putting it on the
on-click
event of the button on the code behind page. I would also like the
error
messages to appear in my Error Summary which is in a panel at the
bottom
of
my page.

This is the event ill probably need to place my code
private void Button1_Click(object sender, System.EventArgs e)
{
//Validate whether a item is selected in dropdown list OR a one/both
checkboxes is checked. Cant allow user to check a checkbox and choose
an
item
from the dropdown list
}

Could someone please help me write this code, im really not sure at
all.

Here is the html code. for the page im using.

<table style="WIDTH: 305px; HEIGHT: 102px" height="102" width="305"
border="2">
<tr>
<td style="HEIGHT: 31px"><asp:dropdownlist id="DropDownList1"
runat="server"
Width="231px">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Blue">Blue</asp:ListItem>
<asp:ListItem Value="Yellow">Yellow</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:dropdownlist></td>
</tr>
<tr>
<td style="HEIGHT: 31px"><asp:checkbox id="CheckBox2" runat="server"
Text="No Colour"></asp:checkbox>
<asp:checkbox id="CheckBox1" runat="server" Text="Any
Colour"></asp:checkbox>
</td>
</tr>
<TR>
<TD><asp:button id="Button1" runat="server"
Text="Button"></asp:button></TD>
</TR>
<tr><td>
<asp:panel id="pnMessagePanel" runat="server" Height="45px"
Width="98.13%">
<asp:ValidationSummary id="vsSummaryOfErrors" runat="server"
Width="897px"
Height="8px" BackColor="#ffffcc">
</asp:ValidationSummary>
</asp:panel>
</td></tr>
</table>

Thanks very much for any help anyone can give me
 
D

Dan Bass

Stephen,

That's a bit clearer, the introduction of a second check box confused me as
to what you were trying to do.

function ValidateDropDownOrCheckBox(sender, args)
{
// if either/box checkbox is selected, and the dropdownlist has no
selection
if ( ( document.forms[0].CheckBox2.checked ||
document.forms[0].CheckBox1.checked) &&
document.forms[0].DropDownList1.value == null )
{
args.IsValid = true;
return;
}

// neither box is checked, but there is a dropdown selection
if ( !document.forms[0].CheckBox2.checked &&
!document.forms[0].CheckBox1.checked &&
document.forms[0].DropDownList1.value )
{
args.IsValid = true;
return;
}

// we get here if:
// - either/both check boxes are checked and there is a selection
// - nothing is checked and nothing is selected
// the above could be shorten by XOR, but this wouldn't be as
readable
// and would be more difficult to follow
args.IsValid = false;
}
</script>



Stephen said:
Sorry but this still doesn't seem to work.
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)

Dan Bass said:
Stephen

you weren't too far off...

Try this:


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>



Stephen said:
I tried the following below and the script kicks in however but no
matter
what I do it won't let me move forward even if I select an item in the
dropdown list or check checkbox2. The error message keeps appearing.

One point of clarification, are you refering to an object being
selected
in
your dropdownlist which is not null?
Just want to check wether something has been selected. Yes then
something
which is not null. I need to change the script slightly as well so as
both
checkbox1 and checkbox2 are included. If checkbox1 &2 are checked that
is
ok
as long as nothing is chosen from the dropdown list. and if an item is
chosen
from the dropdownlist then neither checkbox can be checked.

Can you provide me with a little more help with this. I really
appreciate
your help.

Code im using is as follows: -

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

Onclick event of the button is: -
protected void Button2_Click(object sender, System.EventArgs e)
{
Response.Redirect("WebForm2.aspx");
}

<asp:CustomValidator id="cvRepApp" runat="server" Font-Size="Medium"
ErrorMessage="Must enter either or"
ClientValidationFunction="ValidateDropDownOrCheckBox">
*</asp:CustomValidator>


:

Stephen,

One point of clarification, are you refering to an object being
selected
in
your dropdownlist which is not null?
Note that this is different to whether the dropdownlist is populated
with
items...

If so, then client-side validation would be one way to go.

Write something like the following in the HTML <Head> tags

<script>

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

</script>


Then what you need to do is create a custom validator on your page
(which
would be the error message you want to display), and reference our
javascript function by populating the "ClientValidationFunction" with
"ValidateDropDownOrCheckBox"

This will not let the user click on any post back button until the
args.IsValid is set to true in the javascript function.


Good luck.

Dan.


Could someone please help me with some validation. I have to write
code
which
checks to see whether a dropdown list is populated with a value or a
checkbox
is checked. I want the code to run on the on-click of a button.
My page has 1 dropdown list and two checkboxes. So I either want
the
user
to choose an item from the dropdownlist OR tick a checkbox(it
doesn't
matter
if they tick both checkboxes). I can't allow the user to fill in
the
dropdownlist and tick a checkbox.

Im pretty new to coding and im not sure what is the best way to do
this.
Can
I use a custom validator or would I be better putting it on the
on-click
event of the button on the code behind page. I would also like the
error
messages to appear in my Error Summary which is in a panel at the
bottom
of
my page.

This is the event ill probably need to place my code
private void Button1_Click(object sender, System.EventArgs e)
{
//Validate whether a item is selected in dropdown list OR a one/both
checkboxes is checked. Cant allow user to check a checkbox and
choose
an
item
from the dropdown list
}

Could someone please help me write this code, im really not sure at
all.

Here is the html code. for the page im using.

<table style="WIDTH: 305px; HEIGHT: 102px" height="102" width="305"
border="2">
<tr>
<td style="HEIGHT: 31px"><asp:dropdownlist id="DropDownList1"
runat="server"
Width="231px">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Blue">Blue</asp:ListItem>
<asp:ListItem Value="Yellow">Yellow</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:dropdownlist></td>
</tr>
<tr>
<td style="HEIGHT: 31px"><asp:checkbox id="CheckBox2" runat="server"
Text="No Colour"></asp:checkbox>
<asp:checkbox id="CheckBox1" runat="server" Text="Any
Colour"></asp:checkbox>
</td>
</tr>
<TR>
<TD><asp:button id="Button1" runat="server"
Text="Button"></asp:button></TD>
</TR>
<tr><td>
<asp:panel id="pnMessagePanel" runat="server" Height="45px"
Width="98.13%">
<asp:ValidationSummary id="vsSummaryOfErrors" runat="server"
Width="897px"
Height="8px" BackColor="#ffffcc">
</asp:ValidationSummary>
</asp:panel>
</td></tr>
</table>

Thanks very much for any help anyone can give me
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top