Using this chapter as a guide I have written some validation for a contact form which contains a Comments text box and a number of checkboxes.
I want to ensure that people who fill in the form indicate either an area of interest (any one of the check boxes) or ask a question/make a comment (enter data into the text box)
My client side validation works just fine (A message summary is displayed indicating the appropriate errors in the page):
Code:
Protected Sub InterestComments(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
If txtComments.Text IsNot String.Empty Or ITSupport.Checked = True Or OpenSpace.Checked = True Or Home.Checked = True Or SPDeskTop.Checked = True Or SPServer.Checked = True Or SPSBS.Checked = True Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
I have written the following Java Script for the client side validation (only checking one checkbox as I was testing the principal):
Code:
<script type="text/javascript">
function interestcommentsint(source, args) {
var txtComments = document.getElementById('<%= txtComments.ClientID %>');
if (txtComments.value != '' || !OpenSpace.checked==true)
{
args.isValid = true;
}
else
{
args.isvalid = false;
}
}
</script>
When I click the submit, the error * indicators appear on my page but not the Error MessageBox I get if the Javascript is not called.
My Custom validator looks like this:
Code:
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please indicate an area of interest or enter a comment" OnServerValidate="Interestcomments" Text="*" Display="Dynamic" ClientValidationFunction="interestcommentsint"></asp:CustomValidator>
Testing the form by checking one of the checkboxes being validated serverside but not clientside indicates that the clientside validation is either not running at all or there is an error in the code which is preventing a) the execution of the function and b) the display of the messagebox.
Any clues for me please.