Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

easy one? checkbox validation

Status
Not open for further replies.

dzr

Programmer
Nov 20, 2001
109
US
stumped again!

trying to do validation on a check box - real simple - i just need to be sure the checkbox for the policy agreement is checked before submit is ok.
-the best case scenario i have had is no error, but nothing - it allows submit


-i have tried putting the test for checked in several different places but got errors for any placement other than this one


form code:

<form id="form1" runat="server">
<asp:CheckBox ID="agree" runat="server" />
*By checking this box, I understand the policy stated above.
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator"></asp:CustomValidator>
<asp:Button id="SubmitProgram" style="text-align: center" runat="server" Text="Submit" OnClick="SubmitProgram_Click"></asp:Button>


code behind:

public partial class program_add_Default : System.Web.UI.Page
{

void ServerValidation(object source, ServerValidateEventArgs args)
{
args.IsValid = (agree.Checked == true);
}

protected void SubmitProgram_Click(object sender,EventArgs e)
{

if (Page.IsValid == true)
{
//do some stuff..
}

}

}


 
Does the CustomValidator object have a ControlToValidate inline field? Is so, point it to the checkbox ID.
 
How about checking the checkbox on the button click. If it checked, do your thing, else, display an error message?
 
i tried that... but got an error...

If my checkbox is named "agree" is this correct-->

void ServerValidation(object source, ServerValidateEventArgs args)
{
args.IsValid = (agree.Checked == true);
}


and if so, is the placement -->

inside the "public partial class program_add_Default : System.Web.UI.Page {}"

but not inside the "protected void SubmitProgram_Click(object sender,EventArgs e){}"


??

thanks!

 
something like"
Code:
if (checkbox1.checked) { 
..do your stuff
} 

else { 
display error messge
}
 
it wont even let me use "checked" only _checkchanged ... currently trying to figure that out...
 
i realize i'm being totally dense but ... are you saying the code you used is "not" code behind... as in it goes in the some "<script runat="server" language="C#"></script>" tags in the actual page HEad?


and shouldn't i be able to do this in CodeBehind?


 
ok, got it working.


not sure if this is a visual web developer thing or i'm going to have some serious trouble if I don't deal with this but...

i get "The name "policy_agree" does not exist in the current context" on the checkbox ID plus on the error message label and a few other fields.

all works well though?? files are default.aspx and default.aspx.cs

codefile code is :


if (policy_agree.Checked) {
//do lots of stuff
}
else {
Label1.Text = "Please read and accept the policy statement above.";
}
 
try this:

in ur Page_Load event:
btnSubmit.Attributes("Onclick")="CheckCheckBox('" & agree.ClientId & "');"


in ur JS:
function CheckCheckBox(TheChkBox)
{
event.returnValue=document.getElementById(TheChkBox).checked
}


Note:
1. my code is in VB.NET
2. event.returnValue is IE only...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top