Recent Entries
Contact Doug!
Learn About Doug!
View Doug Boude's online resume
updated 11/18/2009

View Doug Boude's profile on LinkedIn
Link to me!

Follow Doug Boude on Twitter
Follow me!

Be Doug's friend on Facebook
Befriend me!
(I promise not to follow you home)
OO Lexicon
Chat with Doug!
You may also be interested in...
Web Hosting

<< May, 2012 >>
SMTWTFS
12345
6789101112
13141516171819
20212223242526
2728293031
Search Blog

Recent Comments
Re: November 7th, 2012 - Day 1 of the Restoration of America's Greatness (by dougboude at 5/04 9:47 AM)
Re: November 7th, 2012 - Day 1 of the Restoration of America's Greatness (by Simon Magnus at 5/04 9:24 AM)
Re: Providing for Service Layer Objects in CFWheels: Hello Wirebox! (by Brad at 5/03 11:37 AM)
Re: PayPal IPN Coldfusion CFC (by No at 4/29 9:28 AM)
Re: Java: The Journey Begins (by jwilliam at 4/23 11:56 PM)
Re: Providing for Service Layer Objects in CFWheels: Hello Wirebox! (by geirman at 4/21 10:24 AM)
Re: Providing for Service Layer Objects in CFWheels: Hello Wirebox! (by dougboude at 4/20 11:32 AM)
Re: Providing for Service Layer Objects in CFWheels: Hello Wirebox! (by geirman at 4/20 10:27 AM)
Re: The Model-Glue Event Lifecycle in Layman's Terms (by Ashwini at 4/18 2:51 PM)
Re: Viewing Option Text (in IE7) that's Wider than the Select List (by cormac at 4/18 4:18 AM)
Re: Providing For Service Layer Objects in CFWheels (by Chris Geirman at 4/16 2:04 PM)
Re: Why Provide for Service layer objects in CFWheels? (by dougboude at 4/13 9:13 AM)
Re: Why Provide for Service layer objects in CFWheels? (by Eric Cobb at 4/13 8:26 AM)
Re: Why Provide for Service layer objects in CFWheels? (by Chris Geirman at 4/13 6:44 AM)
Re: Why Provide for Service layer objects in CFWheels? (by Per Djurner at 4/12 3:39 PM)
Re: Refreshing Cached ColdFusion Webservices Through the Back Door (by Paul at 4/11 3:26 PM)
Re: Adding and Customizing Mura Editor Styles (by dougboude at 4/05 3:54 PM)
Re: Adding and Customizing Mura Editor Styles (by mike at 4/05 2:28 PM)
Re: Adding Mura Components to a Page Without Cascading (by Suzy Naschansky at 3/28 10:38 AM)
Re: Adding Mura Components to a Page Without Cascading (by Suzy Naschansky at 3/28 4:36 AM)
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS

Powered by
BlogCFM v1.11

04 November 2008
Elegant Approach to Disabling Submit Button on Forms

A while back I had a project that required the submit button of the login form to be disabled unless both the username field AND the password field had values. There are probably several ways to skin that cat, but my good friend Boyan Kostadinov offered a more elegant solution (he's always good for the most elegant approach!), and I thought I'd share it with whoever else may benefit.

This solution uses the Prototype library (you need version 1.6). It also uses a small "login.js" file that Boyan wrote which looks for anything with the class "enableOnEntry" (the key to making this work) and then cycles through the form elements within that class, adding their IDs to an array (actually a delimited list, but it is split into an array later). Boyan then binds event listeners to each of the form fields which, while typing in values, will check to see if the submit button should be enabled or not. Anyway, you can read through the JS file if you like, but here's how to implement it, along with a working demo in this post:

1. acquire and reference the following js files in your template (in this order):
    prototype.js
    login.js
2. ensure that your form tag has the class "enableOnEntry";
3. ensure that your form has exactly one submit button (of type 'submit', not 'button');

That's it!

Working Demo:

For your convenience, here is the code used to create the working sample above:

<script src="/js/prototype.js"></script>
<script src="/js/login.js"></script>
<fieldset style="width:200px;">
<legend>Login Form</legend>
<form class="enableOnEntry" name="login" id="login" onSubmit="alert('form submitting...');return false;">
 Username:<br><input type="text" id="username" name="username" value="" /><br><br>
 Password:<br><input type="password" id="password" name="password" value="" /><br>
 <br>
 <input name="submit" id="submit" type="submit" disabled="true" value="Login" />&nbsp;&nbsp;<input type="reset" value="Clear" onClick="$('submit').disabled = true;" />
</form>
</fieldset> 

 and here are links to the js files:

 

 prototype.js

login.js

Hope it helps.




Posted by dougboude at 5:55 PM | PRINT THIS POST! |Link | 3 comments
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: Elegant Approach to Disabling Submit Button on Forms
I like the idea of submitting a form. One problem I've come up against is the issue of disabling a form that returns a binary rather than new web page. For example, a Report form that has a "generate report" button that streams back a PDF or an Excel document. The problem I get there is that if I disable the form on submit, it leaves the person with a binary and a page that cannot be submitted again (even though the form is visible).

Any thoughts on this? I have not come up with anything good.
Posted by Ben Nadel on November 5, 2008 at 9:11 AM

Re: Elegant Approach to Disabling Submit Button on Forms
@Ben, obvious suggestion, but: just don't disable the form/submit button in that case, or if you've got a global handler which disables all forms onSubmit, make an exception to re-enable the form/button using a local onSubmit function.

But rather than disabling onSubmit, Doug's post is more about an alternative approach to client side validation of required fields. It sounds appealing but my only concern is that it gives less feedback to the user. They see they can't submit the form, but there's nothing explaining why: just a greyed out button. I think Joel Spolsky wrote about this recently, saying that you should never disable menus etc, but instead explain *why* an option isn't available. It may be obvious to the developer (required fields are empty) but not necessarily to the user.
Posted by Julian Halliwell on November 10, 2008 at 2:47 AM

Re: Elegant Approach to Disabling Submit Button on Forms
@Julian - Awesome input! Thanks for commenting.
Posted by dougboude on November 10, 2008 at 7:18 AM

Name:   Required
Email:   Required your email address will not be publicly displayed.

Want to receive notifications when new comments are added? Login/Register for an account.

Time to take the Turing Test!!!

Eleven plus Four equals
Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!