Nleyten@700


A hundred revisions later, and most of the client-side is coming to a close. The log for revision 700 says 'Added small module for logging debug messages (using Firebug's console facilities)', referring to a small module that detects if you are running under Firebug, and if so activates the console logging of debug messages (this code will of course be removed in the final versions).

I spent the last few dozen revisions working on form validation; I came to the conclusion that most validation criteria fall into a very limited number of common patterns, and with this in mind I made a simple framework which can automatically handle most forms (as long as they properly annotated) without the need for form-specific code. This is an example where the functional nature of Javascript really shines. In a more conventional language, you would need a giant switch/case statement to figure out what to with each validation condition. In Javascript, you can simply define an hashtable which maps each condition to a function:

Validator.tests =
{
"empty":        function (elem)
{return elem.value.trim () === "";},
"null":         function (elem)
{return elem.value === "null";},
"toolong":      function (elem)
{return (elem.value.trim ().length > MAX_LENGTH);},
"unchecked":    function (elem)
{return elem.checked === false;}
};

(And yes, you could do a similar thing in languages like C with function pointers, but do you realise how messy the syntax is?)

Anyway, I should be posting some screenshots of the story submission forms and others soon...

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.