SG Dev Corner

SG Dev Corner

On programming, erlang, science, web design, business and politics.

 
 

8 March 2006 - Atheistic Evangelism

Funny reading
Comments (0) - Leave a Comment


2 March 2006 - A Letter to the American Left

From The Nation an interesting article about the american left by Bernard-Henri Levy.

"Nothing made a more lasting impression during my journey through Americathan the semi-comatose state in which I found the American left.

I know, of course, that the term "left" does not have the same meaningand ramifications here that it does in France.

And I cannot count how many times I was told there has never been anauthentic "left" in the United States, in the European sense... "

Read the full article.


Comments (0) - Leave a Comment


2 March 2006 - Javascript Form Validator

One of the most common tasks in a web handling application is form field validation (e.g. checking that some fields are filled, correct email values, ecc...).
This is a tedious and repetitive task, so being a lazy programmer I tried to simplify and automatize it.

I had an old js library handling this task, used also by graphic designers wi th a litle training.
Recently I rewrote completly the code  to improve it.

Here's what I came up with.
Note: this is the first rewrite. The code is evolving, so what is written in this blog entry may not correspond exactly to the software interfaces.

In the zip you find the library, and an html file containing an example. Let's see how the library is used. First of all here's the call to the validation function:
sgCheckForm('frmName', {callback: test(document.frmName.regex, /regex/), email: 'email', notEmpty: ['one', 'two']});

This is the function to call before the form is submitted. The first argument ('frmName') is the name of the form to validate. The second argument is an object containing the rules form the form validation. An optional third argument supplies the language used for error messages if a field is not valid.

The rules object can contain the following properties (more coming soon):

  • notEmpty
  • email
  • callback
The value of each property can be a string containing the name of the field to validate or a list of strings one for each field to validate using the rule represented by the property name. In case of callback rules, the value is either a function to call, or a list of functions. The use of callback rules leaves a great deal of freedom to the user to define custom rules. In the example you see  a callback that check a form field against a Regular Expression. Since this is a quite common task regex will soon become one of the predefined validator rules.

When a validator is called it checks his rule against the field specified. If the field value is not valid, an error message is displayed a false value is returned and validation is stopped. If all fields are valid the true value is returned, so the form can be submitted.

The library uses functional programming techniques as closures and forst class functions to get some flexybility. The same results could be ottained using an object oriented approach, but as you can see also from other entries in the blog I tend to prefer the functional approach.

The library can be improved a lot. It needs more default validators, error checking, better error message customization, etc... So comments and suggestions are wellcome.


Comments (0) - Leave a Comment


1 March 2006 - Erlang: The movie

Erlang: The Movie


Comments (0) - Leave a Comment