It’s a common task to validate a form with JavaScript upon form submission. And validating whether or not at least one checkbox in a checkbox group is checked is often a basic requirement of this task. But, if you’re form is created dynamically and if the number of checkboxes in a checkbox group can vary from 1 to more than 1, then the JavaScript needed to iterate over the checkboxes is slightly different.
Say you have an HTML form like this:
<form name="frm" method="post">
<input type="checkbox" name="parts" value="1" />Head<br />
</form>
Then, using JavaScript, you can check whether the checkbox is checked like so:
var isChecked = document.frm.parts.checked;
Now, imagine that your form has more than one checkbox in the same checkbox group:
<form name="frm" method="post">
<input type="checkbox" name="parts" value="1" />Head<br />
<input type="checkbox" name="parts" value="2" />Neck<br />
<input type="checkbox" name="parts" value="3" />Arm<br />
<input type="checkbox" name="parts" value="4" />Leg<br />
</form>
Then, the JavaScript code changes a little bit, as now you have to iterate over each of the checkboxes.
var isChecked = false;
for (var i = 0; i < document.frm.parts.length; i++) {
if (document.frm.parts[i].checked) {
isChecked = true;
}
}
But. if you tried using the 2nd chunk of JavaScript on the first chunk of HTML, you’ll get a JavaScript error. This is because if you only have one checkbox, there’ll be no array to iterate over. So, you need to do a quick check before hand to see if there is more than one checkbox. If so, then iterate; otherwise, don’t.
if (document.frm.parts) {
if (typeof document.frm.parts.length != 'undefined') {
for (i = 0; i < document.frm.parts.length; i++) {
if (document.frm.parts[i].checked) {
isChecked = true;
}
}
} else {
isChecked = document.frm.parts.checked;
}
}
This code first checks to see that there exists at least one checkbox. Then, it checks whether the length attribute of the parts variable is defined or not. If it is defined, then we know there is more than one checkbox. Otherwise, there is only one.
And there ya have it!
22
Using IFRAME in ASP.NET
So, I’ve basically finished coding the “Google Suggest”-like web user control I was writing for the company I’m doing a co-op work term at. I’m now in the process of integrating it into an existing textbox control that they’ve developed, and that should be finished soon.
One part of the web user control is an iframe, which is used to counteract the much-documented z-index and select box problem in Internet Explorer. By placing the iframe directly below the div that the Google Suggest results are displayed in (as in one z-index less than the z-index of the results div), we can avoid this problem all-together.
But, to make the web user control as reusable as possible, I needed to be able to name this iframe uniquely, incase there are multiple instances of the control on the same aspx page. To do this, I needed to be able to specify the name of the iframe when it is rendered (or guarantee that it would be automagically named uniquely), and I figured out I could accomplish this by using two standard controls: PlaceHolder and HtmlGenericControl.
First, I create the HtmlGenericControl object that will eventually render as an iframe and specify whatever attributes this iframe will have:
// Create new iframe control
HtmlGenericControl searchFrame = new HtmlGenericControl("iframe");
searchFrame.ID = "searchFrame";
searchFrame.Attributes.Add("class", "searchFrame");
searchFrame.Attributes.Add("frameborder", "0");
Then, I can add it to the PlaceHolder’s controls collection:
// Add it to the Controls collection of the PlaceHolder control
searchHolder.Controls.Add(searchFrame);
Finally, I add the PlaceHolder control into my ascx document where I’d like the iframe to eventually be:
<div class="searchContainer">
<asp:PlaceHolder id="searchHolder" runat="server" />
</div>
Now, an iframe will appear in the outputted HTML code where the placeholder once was. But more than that, since I used an ASP.NET control to actually create the iframe, the iframe will be named uniquely. We even know what it’ll be called: this.UniqueID + “_searchFrame”. This is beneficial to me since I can now reference that iframe by name throughout my JavaScript code and show it or hide it when necessary.
19
Rails Book Came Today!
Yay! My copy of Agile Web Development with Rails came in the mail today from Amazon. Time to learn _proper_ Ruby on Rails now.
: )
My co-op work term is coming to an end at my current workplace and I’ve been given the opportunity to produce an ASP.NET web control that functions similar to Google Suggest which will provide me with something that I can write about for my end-of-term technical report. So, not only will I be coding all aspects of the web control, but I’ll also writing a techical paper that details the process that I followed to create the web control as well as the intimate details as to how it’s been implemented. I suppose this’ll give me a chance to practice the technical writing skills that I picked up last term in the 300-level technical writing course that I attended.
Since I’m relatively new to .NET, it should come as no surprise that I’ve never created a web control before, let alone create one that uses AJAX. But I have used AJAX in my Ruby on Rails site before, although I’ll admit that the experience won’t be all that much help unless I decide to use the Prototype javascript framework within my web control.
Anyway, I have already gently scoured the ‘Net for existing web controls that meet my requirements and, while there are numerous projects and code samples that contain bits that I’ll probably examine closer, I wasn’t able to find one that perfectly fits the bill. (I’m actually pretty sure there are some out there and that I was just not able to find them.) In any case, the most difficult part will probably not be the C# but rather the JavaScript, and with the abundance of XMLHttpRequest JavaScript snippets out there, I’m sure I’ll be able to appropriate (and credit, of course!) at least some of them into my work.
In any case, I’m excited about this project and am looking forward to starting on it this weekend. Like my other pet projects, I’ll probably document some interesting snippets of code here, so stay tuned. : )
If you’ve been hosting your app with Textdrive using Apache and FastCGI, you may have noticed that FastCGI seems to crash every few minutes with these kinds of error messages:
[Mon Aug 08 03:40:25 GMT 2005] Dispatcher failed to catch: SIGHUP (SignalException)
/usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:597:in `each’
/usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:597:in `each_cgi’
dispatch.fcgi:18
FCGI process 28217 killed by this error
[Mon Aug 08 04:07:55 GMT 2005] Dispatcher failed to catch: exit (SystemExit)
/usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:10:in `exit’
/usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:10
/usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:10:in `call’
/usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:597:in `each’
/usr/local/lib/ruby/site_ruby/1.8/fcgi.rb:597:in `each_cgi’
dispatch.fcgi:18
FCGI process 38625 killed by this error
These crashes really don’t have _that_ much of an effect on your website, except that the next visitor to your website will have to wait a few seconds before anything appears while the web server caches some information. But my Ruby on Rails website gets quite a bit of traffic, so this happens to visitors every 5 minutes throughout the day.
So, I decided to move to Lighttpd from Apache, which should stop these types of errors from occuring.
The Lighttpd setup was extremely painless and I had everything setup within 30 minutes. The first step is to send a ticket to the support staff at Textdrive, requesting them to setup a Lighttpd port for you. In the reply from the Textdrive support staff, they’ll reference a couple great webpages that contain Lighttpd tutorials to follow. Specifically I followed these tutorials:
- http://wiki.rubyonrails.com/rails/show/DeployingApplicationOnTextdrive
- http://jlaine.net/blog/48/running-your-own-lighttpd-on-textdrive-and-keeping-it-alive-too-part-i
On the Deploying Application On Textdrive wiki page, skip halfway down the page and check out the section called Lighttpd For Idiots. This is a great tutorial that will get you nearly complete. (I did find one small problem with this tutorial though. When I ran the lighttpd command over SSH to start up the Lighttpd server, I got an error message saying that my lyricsdb.socket file didn’t exist. To fix this problem, just create a tmp/ folder in the lighttpd/ folder.
On you’ve completed this tutorial, head to the 2nd webpage that I linked to above. The 2nd link shows you how to set up a cron job so that your Lighttpd server starts every time the webserver is restarted.
Now, my Ruby on Rails website is being served by Lighttpd. I just hope that I didn’t miss anything, considering that my app was initially running over Apache and FastCGI. Do I need to turn anything off that I may have turned on to get Apache and FastCGI going in the first place?
I found this great post on the Ruby on Rails wiki that shows you how to run background jobs in Rails. Specifically, for the nightly maintenance that I’ll be doing on my website, I’ve chosen to follow the “Use cron or the like to run ruby code” method. There is some example code available on the wiki page that shows you how to access your Rails app from a script.
So far, all I’ve done is use the supplied example code as a base for my maintenance script. I’m in the process of adding the actual “maintenance” code at the moment and, as I do, am testing it by running the script from the command prompt: ruby nightly_maintenance.rb.
Are there any “gotchas” that I should be weary of when following this process?
Recommended Services
Recent Posts
- Fantastic new corporate themes for WordPress
- Vistaprint offers FREE t-shirts, too!
- 80+ “Your Ad Here” 125 x 125 banners
- 5 Minute Long Tail SEO Drill: More Traffic, Better SERPs
- iPhone and iPod Touch app statistics: OS adoption, purchasing rates
Recent Comments
- malickakor on How To: Cloak your Affiliate Links for Free in Under 3 Seconds
- Neerav on Using JavaScript to validate one or more grouped checkboxes
- Jacob on Using JavaScript to validate one or more grouped checkboxes
- kaify on Using JavaScript to validate one or more grouped checkboxes
- JC Goldenstein on How To: Cloak your Affiliate Links for Free in Under 3 Seconds
Categories
- .net
- acoustic guitar
- affiliate marketing
- ajax
- amazon associates
- blogging
- books
- business ideas
- c#
- code igniter
- dealdotcom
- google adsense
- google adwords
- internet marketing
- iPhone
- javascript
- leadership
- make money online
- mortgage goal
- msn adcenter
- networking
- personal development
- php
- ppc
- ruby
- ruby on rails
- seo
- text-link-ads
- web development
- yahoo search marketing


