Validate CheckBox

R

Rowena

I can't get my checkbox to validate, I have server and clientside code. Thanks!

<script type="text/javascript">
function CustomValidatorAgree_ClientValidate() {
alert("Yay!");
var chk = document.getElementById("fvMain_chkRulesAndRegs");
var validator = document.getElementById("fvMain_cvRulesAndRegs");
validator.isValid = chk.checked;
}
</script>

<asp:CheckBox runat="server" id="chkRulesAndRegs" Text="I have read and
understood the Rules and Regulations." />

<asp:CustomValidator ID="cvRulesAndRegs" Text="Required!" runat="server"
ValidationGroup="main" OnServerValidate="CustomValidatorAgree_ServerValidate"
ClientValidationFunction="CustomValidatorAgree_ClientValidate"
ControlToValidate="Burn_Address" />

Protected Sub CustomValidatorAgree_ServerValidate()
CType(fvMain.FindControl("cvRulesAndRegs"), CustomValidator).IsValid =
CBool((CType(fvMain.FindControl("chkRulesAndRegs"), CheckBox).Checked))
End Sub
 
S

Scott M.

Rowena said:
I can't get my checkbox to validate, I have server and clientside code.
Thanks!

<script type="text/javascript">
function CustomValidatorAgree_ClientValidate() {
alert("Yay!");
var chk = document.getElementById("fvMain_chkRulesAndRegs");
var validator =
document.getElementById("fvMain_cvRulesAndRegs");
validator.isValid = chk.checked;
}
</script>

<asp:CheckBox runat="server" id="chkRulesAndRegs" Text="I have read and
understood the Rules and Regulations." />

<asp:CustomValidator ID="cvRulesAndRegs" Text="Required!" runat="server"
ValidationGroup="main"
OnServerValidate="CustomValidatorAgree_ServerValidate"
ClientValidationFunction="CustomValidatorAgree_ClientValidate"
ControlToValidate="Burn_Address" />

Protected Sub CustomValidatorAgree_ServerValidate()
CType(fvMain.FindControl("cvRulesAndRegs"), CustomValidator).IsValid =
CBool((CType(fvMain.FindControl("chkRulesAndRegs"), CheckBox).Checked))
End Sub

Neither your client or server side functions are defined correctly. Neither
one has the two required arguments. If they were there, you'd have a MUCH
simpler go of it and wouldn't have to worry about casting. Also, did you
remember to set the following function as the ClientValidationFunction
property of the Validator control?


<script type="text/javascript">
function CustomValidatorAgree_ClientValidate(sender, args) {
args.IsValid = chk.checked;
}
</script>

Your server-side code doesn't show a "Handles" clause. Do you have
AutoEventWireUp set to true or did you snip that piece for brevity or did
you manually type out the stub of the event handler and forget to add the
Handles part? Also, there is no need to use FindControl if your Checkbox is
on the main form. If it is embedded in a container control (are you using a
FormView?), then you will need it.

Protected Sub CustomValidatorAgree_ServerValidate(ByVal source As Object,
ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles
CustomValidatorAgree.ServerValidate
'Use this if the checkbox is on the main form
args.IsValid = chkRulesAndRegs.Checked

'Use this if the checkbox is a child control of another control like a
GridView, FormView, etc.
args.IsValid = CType(fvMain.FindControl("chkRulesAndRegs"),
CheckBox).Checked
End Sub

-Scott
 
S

Scott M.

Correction. The client-side function should be:

<script type="text/javascript">
function CustomValidatorAgree_ClientValidate(sender, args) {
var chk = document.getElementById("fvMain_chkRulesAndRegs");
args.IsValid = chk.checked;
}
</script>

-Scott
 
B

bruce barker

try:

// client side function

function CustomValidatorAgree_ClientValidate(src, args) {
args.IsValid = document.getElementById(src.controltovalidate).checked;
}

// server side function

protected void CustomValidatorAgree_ServerValidate(
object src,
ServerValidateEventArgs args)
{
var chkid = ((CustomValidator) src).ControlToValidate;
var chk = FindControl(chkid) as MyControls.ValCheckBox;
args.IsValid = (chk.Checked == true);
}

// as a checkbox does not support validation, make one

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyControls
{
[ValidationPropertyAttribute("CheckValue")]
public class ValCheckBox : CheckBox
{
public string CheckValue
{
get { return this.Checked.ToString(); }
}
}
}

// in aspx:

<%@ Register TagPrefix="my" Namespace="MyControls" %>
<my:ValCheckBox runat="server" id="chkRulesAndRegs"
Text="I have read and understood the Rules and Regulations." />
<asp:CustomValidator ID="cvRulesAndRegs"
Text="Required!" runat="server"
ValidationGroup="main"
OnServerValidate="CustomValidatorAgree_ServerValidate"
ClientValidationFunction="CustomValidatorAgree_ClientValidate"
ControlToValidate="chkRulesAndRegs" />


-- bruce (sqlwork.com)
 
S

Scott M.

bruce barker said:
try:

// client side function

function CustomValidatorAgree_ClientValidate(src, args) {
args.IsValid = document.getElementById(src.controltovalidate).checked;
}

Will the client-side "src" parameter have a "controltovalidate" property as
it will be passed as a JavaScript object, rather than a .NET object?

-Scott
 
B

bruce barker

yes, as long as a ControlToValidate was specified on the server side,
also note the lower case on client side. Unfortunately, MS gave no way
to supply the function that calcs the value passed by args (args.Value)
for any custom control.

-- bruce (sqlwork.com)
 
S

Scott M.

Interesting. Thanks Bruce.

-Scott

bruce barker said:
yes, as long as a ControlToValidate was specified on the server side, also
note the lower case on client side. Unfortunately, MS gave no way to
supply the function that calcs the value passed by args (args.Value) for
any custom control.

-- bruce (sqlwork.com)
 

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

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top