Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 8th, 2010, 02:30 PM
Authorized User
 
Join Date: Jul 2009
Posts: 61
Thanks: 15
Thanked 1 Time in 1 Post
Default Chapter 9 - Validating user Imput (p304)

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.
__________________
Geoff Baldwin
 
Old March 8th, 2010, 03:16 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Geoff,

Take a look at this:

if (txtComments.value != '' || !OpenSpace.checked==true)

OpenSpace is the *server side* ID of the checkbox. It doesn't magically become available as a variable or object reference in *client side* JavaScript. Instead you need to the same as you're doing for the TextBox: get a reference to it using JavaScript's getElementById:

Code:
 
var txtComments = document.getElementById('<%= txtComments.ClientID %>');
var chkOpenSpace = document.getElementById('<%= OpenSpace.ClientID %>');
      if (txtComments.value != '' || !chkOpenSpace.checked==true)
Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
SouthendSupporter (March 9th, 2010)
 
Old March 9th, 2010, 05:24 AM
Authorized User
 
Join Date: Jul 2009
Posts: 61
Thanks: 15
Thanked 1 Time in 1 Post
Default

Thanks Imar
I also discovered that args.IsValid is case sensitive
All works a treat now - thanks again
__________________
Geoff Baldwin





Similar Threads
Thread Thread Starter Forum Replies Last Post
ch-9 Validating user i/p. pg no-309 & 315 mr malik BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 February 17th, 2010 11:21 AM
user password validating cooky4 VB How-To 15 May 4th, 2005 08:45 AM
Image imput button XXL Classic ASP Basics 14 January 9th, 2005 05:50 PM
validating user input hosefo81 Javascript How-To 12 March 3rd, 2004 09:32 AM
Validating user input stu9820 VB.NET 2002/2003 Basics 2 January 15th, 2004 12:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.