DJ's Blog

David Jeavons' Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

JunJuly 2006Aug
SMTWTFS
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

Archives

Blogs I read

Monday, July 03, 2006 #

Well, I must have spent about an hour today trying to figure out something that I assumed would be pretty trivial, in fact it was in the end but you know how it goes.

I was creating a simple registration page for a website which requires that the user agrees to the Terms and Conditions of the site before they can submit their registration details. To begin with, I had a check box that the user would have to tick in order to state that they agreed and only when this check box was checked would the submit button be enabled, however, as I had disabled the submit button on the load of the page, all other required field validators stopped working. I assume as the submit button was disabled, ASP.NET didn't feel the need to continue wiring up the validation code necessary. So I thought that I would use one of the validation controls to ensure that the check box was checked thus no need to disable the submit button.

After a little research, I found that the CustomValidator control would do the job for me, but nearly all of the examples I found seemed to either suggest that you had to write code in your code behind/beside file to register the necessary javascript on the page which I thought was a bit odd, or they just seemed overly complex. After a little playing around with various code snippets, I came up with what I thought was rather simple and not overly complex. Shame I didn't find it in the first place, it would have saved me a fair bit of time.

To test this out, create a new web form file and add a check box to the page named "chkTandCs", then add a CustomValidator control next to it and finally a button. In the source of the web form, add the following javascript function to the head section:

<script language="javascript" type="text/javascript">
    function
ValidateTandCs(source, args)

    {

        args.IsValid = document.getElementById(
'<%= chkTandCs.ClientID %><%= chkTandCs.ClientID %>').checked;

    }

script>

Now, modify your CustomValidator control so that it reads:

<asp:CustomValidator ID="valTandCs" ClientValidationFunction="ValidateTandCs" runat="server" ErrorMessage="Please accept Terms and Conditions before submitting.">asp:CustomValidator>

Note the ClientValidationFunction property is set to the javascript function ValidateTandCs. If the check box is checked then the function in effect returns True and the page is posted, otherwise, the ErrorMessage property of the CustomValidator is displayed and the page is not posted. This works in exactly the same way as a normal RequiredFieldValidator but allows you to validate a check box. In essence, you could use this technique to validate any server side control.

To prove that it definitely worked, I added the following code to the click event of the button:

Response.Write("Page posted back")

Which only writes out to the page once it is posted to the server.

I certainly learnt something new today :)

posted @ 11:10 PM