How NOT to Code - Challenge 13

Feb 11, 2010

Today's challenge is in Javascript. See how many things you can pick out that's wrong with this code ;)

<script language="JavaScript">
<!-- Begin
function checkFields() {
   missinginfo = "";
   if ( == "") {
      missinginfo += "\n - Name";

   if (missinginfo != "") {
      missinginfo = "Correctly fill in your:\n" +
      missinginfo + "\n and submit again!";
      return false;
      return true;


Garrett Johnson

Garrett Johnson wrote on 02/11/104:15 PM

Oh man...

1) Bad comment wrapped around.

2) Global variable no no no no no.

3) document.form will error, its an array so technically should be document.forms[0], but really you should just use an Id to speed things up...

4) Don't use the "==" or "!=" equality operators, use the strict ones: "===" and "!==".
Peter Boughton

Peter Boughton wrote on 02/12/108:04 PM

Here's some more...

5) Language attribute is wrong.
6) Braces are not on own lines.
7) Badly named variable, no word split.
8) Form value is not trimmed.
9) Is using a string instead of an array.
10) Text formatting being done in validation.
11) Double negative for second conditional.
12) Reusing previous variable for error message.
13) Message is really badly worded.
14) Using JS alert for validation.
15) Not applying a CSS style to highlight the invalid field(s).
16) No server-side validation performed.

Ok, so can't *prove* that last one, but it's probably the case. ;)

Write your comment

(it will not be displayed)