Custom Validator to validate certain items checked in a checkboxli

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If possible, how can I use a custom validator to check (client side) that if
a certain value was checked in a checkboxlist? So, if they checked "great" or
"good", comments are not required and if they checked "not so great" or
"awful", comments would be required before allowing the page to post.
 
Handle the ServerValidate method of your CustomValidator control as follows.
Set the args.IsValid equal to True if everything tests to be correct,
otherwise set it false.


Private Sub CustomValidator1_ServerValidate(ByVal source As
System.Object, ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
CustomValidator1.ServerValidate
Try
If chkGreat.Checked Or chkGood.Checked Then
args.IsValid = True
ElseIf chkNotSoGreat.Checked Or chkAwful.Checked Then
args.IsValid = (txtComments.Text.Trim() <> "")
Else
args.IsValid = False
End If
Catch ex As Exception
'whatever
End Try
End Sub
 
Oh, and if you're specifically looking for client-side validation, if it
involves more than one control, you'll have to manually write some javascript
validation routine and call it on form submission.
 
Back
Top