Javascript form validation – oldie but goodie

11 05 2007

I really like what javascript can do, but I hate working with it. My problem is I know just enough to be dangerous, as the saying goes, and I often am.

I have had to do some different form validation over the years and the following code works pretty well out of the box. The snippet below will validate everything, and with some adjustments will validate as you see fit. My guess is there are better, more condensed, ways to get this done (I stumbled across this bit of code several years ago) but it still works well for me and seems to run fine in IE and Firefox.

If I’m performing some gross injustice by using this code please say so in the comments along with what I should be using, if you would be so kind.

function validateForm (form) {
for (var e = 0; e < form.elements.length; e++) {
var el = form.elements[e];
if (el.type == 'text' || el.type == 'textarea' ||

el.type == 'password' || el.type == 'file' ) {

if (el.value == '') {

alert('Please fill out the text field ' + el.name);

el.focus();

return false;

}

}

else if (el.type.indexOf('select') != -1) {

if (el.selectedIndex == -1) {

alert('Please select a value of the select field ' + el.name);

el.focus();

return false;

}

}

else if (el.type == 'radio') {

var group = form[el.name];

var checked = false;

if (!group.length)

checked = el.checked;

else

for (var r = 0; r < group.length; r++)

if ((checked = group[r].checked))

break;

if (!checked) {

alert('You must select an option to continue');

el.focus();

return false;

}

}

else if (el.type == 'checkbox') {

var group = form[el.name];

if (group.length) {

var checked = false;

for (var r = 0; r < group.length; r++)

if ((checked = group[r].checked))

break;

if (!checked) {

alert('Please check one of the checkboxes ' + el.name);

el.focus();

return false;

        }

      }

    }

  }

  return true;

}
Advertisements




.Net app on the desktop or browser based app?

10 05 2007

I’ve been thinking more about the .NET development and am wondering how easy it is to convert a .NET app into a browser-based app.

What are the benefits to having a full blown .NET (desktop) app, vs. a browser-based version that has the same functionality?

My questions around all of this are pointed toward Microsoft and the associated licensing costs. Alternate desktop flavors are maturing at a pretty aggressive rate and will be better a year from now that they are today. More than 90% of our users use the most basic functionality within the productivity apps, Word, Excel, etc. and could function just fine with an open source alternative.

With that said I don’t expect our backend technologies to be changing in the near future. The use of Exchange, IIS, SQL will likely continue. The costs associated with maintaining a backend environment is small when compared to the desktop environment.

Another option may be to stop upgrading the desktops once a Microsoft enterprise or select agreement expires. This is a good solution if we refrain from developing Windows based apps that require upgraded OS’s or .Net frameworks.

What do you think. Desktop or Browser based?





ColdFusion newbie, three years in the making

10 05 2007

I’ve been dabbling in web development since 1996. Never anything fancy or complicated but I have moved through various stages of HTML and ASP eventually landing in the ColdFusion world a few short years ago. The decision to move to ColdFusion was an interesting one and was prompted by the sister of a friend at work.

She had been doing development work on a much grander level than I and upon being asked by her brother how best to develop an app quickly she directed us toward Coldfusion. She had been using it herself for a couple of years and was pretty impressed by it. So we began to investigate.

Initially what surprised me was the size of the ColdFusion community, small, and the number of resources available, even smaller. What impressed me was the quality of the resources and the loyalty of the community. It was readily evident that those developing in CF were older, more mature if you will. They were established in existing corporations and many of them had been there for some time. It was difficult to compare CF, from a demographic perspective, to competing technologies due to the large volume of developers working in ASP and the growing number working in PHP. It certainly is not, nor was not, apples to apples.

With my developer cap slightly askew I took a look at the code and associated tags and found that I was able to reproduce in CF, with far fewer lines, what I was already doing in ASP. Nice. The tags also seemed intuitive and as a result they felt like they required less thought. I hope that makes some sense. They just didn’t seem as heavy but came across just as powerful. You know in the movie ‘A Knights Tale‘ when the lady armourer made the sword out of the new lighter metal and William Thatcher didn’t think there was any way it would be as strong as the traditional sword? The sword turned out to be much stronger. This is how ColdFusion felt to me.

ColdFusion just keeps getting better, as well as continuing to get more and more flack in the design community at large. While all this is happening there is a strong CF Open Source interest taking hold in a CF community that is as passionate as ever, if not more so.

So yeah, I’m still in the newbie camp where CF is concerned. There just aren’t enough hours in the day for me to learn it all and pay attention to those other tasks that are under my umbrella. I do learn enough to get by and am grateful to the solid resources that exist.

A few CF resources:

Adobe ColdFusion
Ben Forta’s Blog
cfsilence
cfweekly (podcast)
ColdFusion Cookbook
ColdFusion Open Source
Pete Frietag
Raymond Camden
Remote Synthesis
RIAForge
Rob Ghonda