Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion 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 June 18th, 2012, 01:39 PM
Authorized User
 
Join Date: Dec 2011
Posts: 39
Thanks: 9
Thanked 0 Times in 0 Posts
Default To anyone wanting a Google-like input form...

I had some time to kill and I reverse engineered Google's input form to my own liking.

I want to post this because I had a hard time figuring out how to add a special error class to a control that failed validation so that it would have a red outline on it. There are tons of examples online but many of them didn't work or were extremely verbose. This example is the most elegant imo.

Client side-code

Code:
<script type="text/javascript">
      //<![CDATA[
      function ValidateControls(source, args) {
         // validation logic here
         if (args.Value == "") {
            args.IsValid = false;
         }
         else {
            args.IsValid = true;
         }

         // change control's color to red if failed
         var ctrl = $('#' + source.controltovalidate);

         if (!args.IsValid) {
            if (ctrl)
               ctrl.addClass("errorBox");
         }
         else {
            if (ctrl)
                ctrl.removeClass("errorBox");
         }
      }
      //]]>
  </script>
XHTML exmaple

HTML Code:
      <span>First Name</span>
      <asp:TextBox ID="TxtFName" runat="server"></asp:TextBox>
      <asp:CustomValidator ID="CVTxtName" runat="server" Display="Static" 
              ValidateEmptyText="true" ControlToValidate="TxtFName" 
              ClientValidationFunction="ValidateControls" CssClass="validators"
              ErrorMessage="Enter your first name." onservervalidate="ValidateRequiredField">
      </asp:CustomValidator>

And like Imar's book says, I also validate at the server side

Code:
protected void ValidateRequiredField(object source, ServerValidateEventArgs args)
    {
        // if field is empty signal validation fail
        if (!string.IsNullOrEmpty(args.Value))
            args.IsValid = true;
        else
            args.IsValid = false;
    }

    protected void LBSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            // submit records
        }    
    }

If anyone has any critiques on this code please feel free to comment, especially Imar. I'm sure you have an even better solution for this. ; )

Last edited by IceThatJaw; June 20th, 2012 at 01:18 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending VBA generated email via Google Mail (Google apps) Nyanko Excel VBA 2 February 9th, 2013 10:44 AM
Wanting to call excel function for quartile in acc webmagic00 Access 1 July 22nd, 2008 07:25 AM
Google toolbar visually interfering with form mat41 CSS Cascading Style Sheets 6 January 17th, 2008 06:11 PM
Been wanting 2 know this for ages! Apocolypse2005 Beginning VB 6 9 September 21st, 2007 11:36 AM
I am wanting to start developing for Pocket Pc 5.0 iampedro Intro Programming 0 July 17th, 2006 12:01 AM





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