[cgiapp] Unobtrusive Javascript form validation

Joshua Miller unrtst at gmail.com
Thu Jul 8 22:00:03 EDT 2010


On Thu, Jul 8, 2010 at 4:35 AM, Richard Jones <ra.jones at dpw.clara.co.uk> wrote:
> the popup message says 'Website is not valid'. But I like the easy
> customisation of messages, and the output is a little more refined than
> the output from my implementation of JavaScript::DataFormValidator. I

For what it's worth, you can fully customize the messages displayed
from Data.FormValidator. There were a few people I worked with that
didn't understand the usage, as it's not completely obvious at first,
but it is very flexible.

For example, from the docs, if your validation profile in perl is:

my $profile = {
    optional => [qw( company age fax country )],
    required => [qw( fullname phone email address )],
    constraints => {
        email => { name => "valid_email",
                   constraint =>
"/^(([a-z0-9_\\.\\+\\-\\=\\?\\^\\#]){1,64}\\@(([a-z0-9\\-]){1,251}\\.){1,252}[a-z0-9]{2,4})$/i"
},
        age => { name => "valid_age",
                 constraint => "/^1?\d?\d$/" },
    },
    msgs => {
        constraints => {
            valid_email => "Invalid e-mail address format",
            valid_age   => "Age entered must be between 0 and 199",
        }
    },
};


Then if the "email" or "age" constraints fail, the message displayed
will come from the "msgs" hash, using the "name" from the constraint
to look it up.

"email" being the field name, it would trigger the "email" constraint.
If that fails, it looks at the "name" from the constraint, which is
"valid_email" in this case, and looks that up in the
"msgs->constraints" hash, to print out "Field [email]: Invalid e-mail
address format".

See the demo here: http://formvalidatorjs.berlios.de/ex1.html
and fill in the first text are with some random text, and hit submit.

Sorry for heading off on a tangent here. I'm a little biased in favor
of Data.FormValidator, though I'm always interested in seeing what
other solutions people come up with, and I can readily admit it
doesn't do everything. For example, it's not good at complex
validation on the frontend, though I think that'd be possible with
some updates.

The company I work for has also been moving to JQuery, so I won't be
surprised if Data.FormValidator gets updated at some point to
integrate better with JQuery, possibly on a fork.

Thanks,
--
Josh I.


More information about the cgiapp mailing list