Simple Rails Javascript Form Validation
Sun Mar 02 18:57:26 -0800 2008
If you have a form with some required text fields (like, name, password) then waiting for your users to hit submit is too late to tell them those fields are required. The other option is disabling the submit button until they have filled in the data, here is how…
First, you need to have the javascript default libraries loaded in Rails to make this work.
Once you have done that, then open up your public/javascripts/application.js and enter the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
document.observe('form:button_disable', function (event) { // Find the button that we want to disable var button = $('ButtonDomID'); // Disable it! button.disabled = true; // Find the field that is required: var required_field = $('FieldDomID'); // Set up an observer to monitor this field new Field.Observer(required_field, 0.3, function() { // If field == '' then button disabled = true button.disabled = ($F(required_field) === ''); }); }); |
And that’s it!
You should also put a little note at the bottom under your submit button that says it won’t work until you enter something.
You can put whatever logic you want into the Field.Observer, for example, replace the button.disabled = line with the following and it will check to see it has a minimum length as well:
1 2 3 |
length_ok = ($F(required_field).length >= 10);
not_empty = ($F(required_field).value !== '');
button.disabled = !(length_ok && not_empty); |
blogLater
Mikel
Leave a Reply
Latest posts
- Encrypting Another Partition Using FileVault 2 on OSX Lion
- Installing Home Folder on Second Drive on OSX Lion
- undefined local variable or method `version_requirements'
- A New World of Resources
- Rails Static Pages
- Twitter Replacing Rails? So?
- Engine Yard Cloud Backups Generating Zero Length Backups
- Our Rails Rumble Entry - StillAlive.com
- Renaming a controller and redirection in Rails 3
- Updating RailsPlugins.org to Rails 3 - Part 1
- A new protocol for social interaction
- What is a distributed social network?
- Bundler and Public Git Sources
- Getting Heroku, memcached and Rails 3 working
- Why Bundler?
- Rails Commit Access
- Introducing TellThemWhen
- rake RSpec & Cucumber uninitialized constant Rails::Boot::Bundler
- This Relationship is Worth Nothing
- Thank YOU...
- Inline Attachments for ActionMailer
- Upgrading RailsPlugins.org to Rails 3 - Part 1
- Stripping dollar signs and commas from a string
- Getting Rails 3 Edge with jQuery, RSpec and Cucumber using RVM
- Action Mailer, go Proc thyself
- The Real News Donation Drive
- ActionMailer ScreenCast and Article
- Installing RSpec for Rails 3
- I am speaking at RailsConf 2010
- Rails 3 Session Secret and Session Store
- If you're lazy and you know it write your specs!
- Bundler - uninitialized constant ActionController
- Bundle Me Some Sanity
- How to use Mail / ActionMailer 3 with GMail SMTP
- Put your mailer where the action is!
- Why Force a Choice?
- How to make an RSS feed in Rails
- Rails 3 Routing with Rack
- Bundle me some Rails
- Helping out in Haiti
- Watch your self
- Is Rails 3.0 a Game Changer?
- Where did the scripts go?
- validates :rails_3, :awesome => true
- New Rails Version 3.0 Guides Online
- New ActionMailer API in Rails 3.0
- Mail gem version 2 released
- How to rename a Rails 3 Application
- Rails 3.0 Examples
- DECCA Driving Day
Latest comments
- buy camel cigarettes
Wonderful blog! I definitely lo...
- how to become a pilot
About this topic, I have been l...
- how to become a pilot
About this topic, I have been l...
- best cigarettes
This was a fantastic post. Real...
- best cigarettes
This was a fantastic post. Real...
Categories
Tag Cloud
AJAX ARGH! ActiveRecord Ajax Apache Apple Asterisk Australia Copy Database Development Feedburner Gem server Google Human Rights Javascript L. Ron Hubbard MS SQL Server MacOSX Mail Mephisto Not Programming OpenBSD Opensource Performance Personal Integrity PostgreSQL Programming Prototype Puzzle RDoc REST RESTful Rails RSPec RSpec Rails Rails Tips Rspec Ruby Ruby on Rails Ruby on Rails Tips Ruby on rails Tips SQL SQLServer SVN Scientologist Scientology Site Stats Soekris Soekris net5501 TMail Textmate Tips Windows World about mikel anti drug apache contributing daemon documentation drugs illustrator javascript lambda mail mephisto newspapers nitro open source opensource photoshop productivity programming railscasts rspec ruby ruby on rails rubyforge scientology seo sitemap sqlserver tips tmail tom cruise unix tricks vector graphicsArchives
- November 2009 (1)
- October 2009 (2)
- September 2009 (2)
- August 2009 (0)
- July 2009 (1)
- June 2009 (0)
- May 2009 (1)
- April 2009 (0)
- March 2009 (0)
- February 2009 (0)
- January 2009 (2)
- December 2008 (0)
- November 2008 (5)
- October 2008 (0)
- September 2008 (1)
- August 2008 (0)
- July 2008 (2)
- June 2008 (13)
- May 2008 (7)
- April 2008 (18)
- March 2008 (8)
- February 2008 (5)
- January 2008 (7)
- December 2007 (20)
- November 2007 (22)




Tue Mar 18 07:01:48 -0700 2008
What about the onsubmit attribute?
returning false stops submission.
Tue Mar 18 08:15:12 -0700 2008
Greg: Good point, though on this form I did not want the user to be able to hit the submit button at all – ie, have it disabled until the requirements were met.
But that would work if you wanted to pop up an alert or some other client side processing before the form was submitted.
Thu Apr 24 05:56:39 -0700 2008
Is there a DRY way to do this? If I’ve already setup all my validation rules in my models, then it’d be nice if I could get Javascript validation “for free”.
Fri Apr 25 10:35:18 -0700 2008
Turns out there is a more complete way to do it:
http://livevalidation.rubyforge.org/
This uses the live validation javascript library. I haven’t used it, but looks cool.
Mikel
Tue Apr 29 17:33:55 -0700 2008
@mikel:
The livevalidation plugin is still very green. Basically, it’s unusable if you have any sort of validation within a validate method. In addition to this you’re gonna have to hack the plugin to get it to display the errors in a way you’d like.
I got so annoyed with it that I went back to just hacking the livevalidation JS!
Thu May 01 09:05:01 -0700 2008
Nice post, pardon my denseness but what would be the process of adding multiple required fields? Is there an easy way in prototype to just make an array and loop through it?
Thu May 01 21:32:17 -0700 2008
@blaine – Absolutely, you want the prototype “each” function, works almost exactly the same as Ruby’s each, you can find out more on the prototypejs.org/api pages.
Then you just apply that true / false to each element of the form. It’s a bit more complex, but I do it on another form for similar things.
Check out my “field monitor” page (it is in the side bar under pages). This does something similar to what you are after.
Mikel
Tue Aug 12 13:27:57 -0700 2008
I m getting javascript error in firebug console saying “document.observe is not a function”
but i included <%= javascript_include_tag :defaults %> in header
Sun Oct 19 22:45:42 -0700 2008
Not real sure what should go here: ‘form:button_disable’. Can’t get this to work for me but love the idea. Disclaimer: not a javascript guru.
Thanks in advance for any help!