checkbox validation

N

Ned Balzer

I posted this this morning but it never went through, so I am trying
again -- apologies for the duplication if so.

I need to validate several checkboxes on an asp.net 2.0 page. I don't
need to validate that they are checked; instead, I am trying to
prevent potential SQL or code injection via checkbox controls. These
checkboxes are inside a formview.

I found examples (e.g at http://msdn2.microsoft.com/en-us/library/aa479013.aspx)
of how to validate that at least one in a group of checkboxes, or one
of a checkboxlist, has been checked, but my requirement is a little
different.

What I'd like to do is write a server side and client side functions
that several customvalidation controls can share. So the function
would determine which checkbox control is failing validation and
respond appropriately.

The example code looks like this:

<script runat="server">

Sub CustomValidator1_ServerValidate(source As Object, _
args As ServerValidateEventArgs)

args.IsValid = (CheckBox1.Checked = True)
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)
If Page.IsValid Then
Label1.Text = "Thank you for your donation!"
Else
Label1.Text = ""
End If
End Sub

</script>
....
<asp:CheckBox id="CheckBox1" runat="server"
Text="Donate $10"></asp:CheckBox>
&nbsp;
<asp:CustomValidator id="CustomValidator1"
runat="server" ErrorMessage="Please donate $10"
OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>

The problem is that the checkbox control is referenced in Sub
CustomValidator1_ServerValidate by its ID, but I'd like the server
side validation subroutine to handle any number of different
checkboxes.

Is it possible to determine which checkbox is failing validation by
examining either of the two parameters passed to Sub
CustomValidator1_ServerValidate? (either "source" or
"ServerValidateEventArgs")?

Thanks.

-- Ned Balzer
 
M

Mohamad Elarabi

Ned, I don't think what you're trying to do is possible because in order for
the information about the checkbox to be passed into the arguments you will
have to set the ControlToValidate attribute on the CustomValidator, but then
that will lock you down to one control to validate. However there might be a
workaround.

If you prefix the IDs of the checkboxes you want to validate with something
unique like "cbx" you can then loop through the page controls and identify
the controls with cbx in their ids and then do your validation on each one
and update the IsValid and the ErrorMessage accordingly. Your serverValidate
should look something like this

Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As _
ServerValidateEventArgs)
For Each oControl As System.Web.UI.WebControls.WebControl In Page.Controls
If Left(oControl.ID, 3) = "cbx" Then
If CType(oControl, CheckBox).Checked Then
args.IsValid = args.IsValid And (CType(oControl, CheckBox).Checked =
True)
SysMessage.ErrorMessage &= oControl.ID & " is not valid.<br/>"
End If
End If
Next
End Sub

Please let me know if this helps. Good luck.
 

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

Staff online

Members online

Forum statistics

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

Latest Threads

Top