Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #9815

Re: eval() or What?

Date 2011-12-31 00:17 +0100
From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: eval() or What?
References <fn8nf756jclubi6uu8510fcqjt57lhcd15@4ax.com> <9m1pc5F743U1@mid.uni-berlin.de> <9tgnf7ps2um6d8bubb8725kqlakckln11i@4ax.com> <9m4505F7d7U1@mid.uni-berlin.de> <p2ppf7d7qp2frnllpjna3mp80cq3lnlpcv@4ax.com>
Message-ID <2sCdnS_ena_r22PTnZ2dnUVZ8omdnZ2d@giganews.com> (permalink)

Show all headers | View raw


On 2011-12-29 23:08, Gene Wirchenko wrote:
> On Thu, 29 Dec 2011 22:39:17 +0100, Jake Jarvis
>>It reads like having two string value properties containing program code 
>>to be eventually evaluated one way or another is a given and not up to 
>>reconsideration, if that is indeed the case, why is that so?
> 
>     It is simply the design that was most obvious.  I did not exclude
> anything else from consideration: look at my points 2 and 3 in my
> original post.

It doesn't seem obvious to me at all. In fact, when I read "the
properties will have string values of code to be executed", a big red
flag goes up in my mind. There are very few designs which require code
to be evaluated on the fly. A form validator seems like an unlikely
candidate (unless you want your users to enter custom validation code,
or you have other external code sources).

In my experience, 90% of validation tasks can be easily generalized. For
example:

- has the field been filled out?
- does the entered value have the correct format?
    - numeric
       - integer
       - positive/negative
       - precision
    - monetary amount (maybe with currency)
    - email address
    - phone number
    - date
- does the entered value exceed defined boundaries?
    - min/max values for numbers, dates
    - min/max string length
- does the entered value match a given regular expression?

This is not comprehensive, of course, it's just off the top of my head.
Your application will likely have other typical formats which can be
generalized (customer numbers, case numbers, etc). The point is that
these general routines can (and IMHO should) be written as methods of
your validation library, instead of being evaluated as raw code. In the
remaining 10% of cases where automated validation is not (easily)
possible, you can provide a custom callback function.

Instead of directly manipulating local variables, I would define an API
for each of the validation modules/routines. At the very minimum, they
should receive the DOM element being validated or its value, and the
validation options (see list above). As for the return value, this could
be true/false for success/failure, or an error string, an error code, or
whatever else matches your needs.

I have written several form validator objects in the past; they're quite
different from each other, depending on the customer and the framework
they were supposed to fit into. As a minimal (simplified) example, the
validation script for a form with two fields might look like this:

  var fv = new FormValidator(document.forms[0]);
  fv.addWatchedFields({
      datePublished: {
          type: "date",
          min: "1980-01-01",
          max: "today"
      },
      title: {
          type: "string",
          required: true,
          max: 200    // characters
      }
  });

The FormValidator will then monitor the "datePublished" and "title" form
elements. Whenever a value has changed (or the form is being submitted,
or the field sends a blur event, etc), it can call a method like

   function checkWatchedField (node, opts) {
       if (opts.type == "date") {
           return checkDateField(node, opts);
       } elsif (opts.email) {
           return checkEmailField(node, opts);
       }
       // etc
   }

That's just one of many ways to implement a validator. The important
thing is that there is no need for eval()ing code anywhere; it's all
just methods of a FormValidator object.


- stefan

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

eval() or What? Gene Wirchenko <genew@ocis.net> - 2011-12-28 15:29 -0800
  Re: eval() or What? Jake Jarvis <pig_in_shoes@yahoo.com> - 2011-12-29 01:08 +0100
    Re: eval() or What? Gene Wirchenko <genew@ocis.net> - 2011-12-28 17:42 -0800
      Re: eval() or What? Jake Jarvis <pig_in_shoes@yahoo.com> - 2011-12-29 22:39 +0100
        Re: eval() or What? Gene Wirchenko <genew@ocis.net> - 2011-12-29 14:08 -0800
          Re: eval() or What? Stefan Weiss <krewecherl@gmail.com> - 2011-12-31 00:17 +0100
  Re: eval() or What? Dr J R Stockton <reply1152@merlyn.demon.co.uk> - 2011-12-29 19:57 +0000

csiph-web