Published 2004-12-02 18:55:39

I've been working on a typical web application, and given the fact that deadlines are not a big issue for a change, a little focus has gone into thinking 'is there a better way to do this?'

The challenge is really, how to do client side javascript validation and at the same time add the ability for flexy to pick up the calls and perform the matching serverside validation..

Like alot of my client side javascript, i'm going with a model that basically involves adding these lines to the end of the file:

<script type="text/javascript" src="/js/validate.js" / >
<script type="text/javascript">

The onload method adds a onsubmit hander to all the forms, and which in turn looks for all the input elements and does the required validation.

I've tended to avoid onchange handlers on the form, partly as I feel it is a bit confusing for an end user to be continually be reminded that they have got something wrong, even while they are filling in the form (and perhaps havent finished entering the data)

the onload'er method is also quite nice, as it doesnt involve to many changes to the form.

Now comes the crunch.. where to add the checks for validation, A long while ago I tried something like this:
<input name="age" validate:rule="isempty();number({ min: 8, max: 100});">
Using javascript, it should be easy to extract the rule, split it on the ');' bit, and eval each of the expressions, using the input's value.

On the Flexy side, it does involve a little messing around to convert the tests to pear's Validate class, but the principle is quite nice in the fact you can test the validation just by viewing the template, without actually installing it in the application and running it through the template engine.

Javascript allows me nicely to change the background colour if there is an error (either directy or setting it's style), but again another issue raises its head:
Where do I put the message about what is wrong.
  • On the simple version you really want it to stick add a div layer, and put a message under the box.
  • On a more complex version, you want it to pull the phrase from a hidden element on the page, and display a translated version if applicable.
The other thing to consider is how to re-integrate the error messaging with stuff coming back after a post (on the extremely unlikely situation these days that someone has turned of javascript )

But at least it did make me think that I should javascript enable the comment boxes on my blog, only making the comment box appear if you are using javascript.. - at least it should stop a few blog spammers.

Mentioned By: : DB_DataObject Error: Connect failed, turn on debugging to 5 see why ( referals) : april ( referals) : Connect failed, turn on debugging to 5 see why ( referals) : december ( referals) : flexy javascript ( referals) : javascript validation ( referals) : "DB_DataObject Error: Connect failed, turn on debugging to 5 see why" ( referals) : validating javascript ( referals) : XMLHttpRequest php ( referals) : "Connect failed, turn on debugging to 5 see why" ( referals) : javascript validate number ( referals) : xmlhttp php ( referals) : PHP Buzz Forum - Validating javascript and flexy ( referals) : AKBK home - Smoking toooooo much PHP - Validating javascript and flexy ( referals) : "DB_DataObject Error: Connect failed" ( referals) : DB_DataObject Error: Connect failed ( referals) : javascript validate ( referals) : "turn on debugging to 5 see why" ( referals) : Connect failed, turn on debugging to 5 ( referals) : "turn on debugging to 5 see why " ( referals)


Javascript interact with PHP
Please try do a google on 'xmlhttp'
I swear to God this is really good.
It allows real time Javascript interaction with server side script.
I managed to get it to work with my PHP script.
Tested it on Firefox and IE, worked like a charm.

To see this in action, check out Google Suggest:
They are using onkeyup as an event to send query to the server side script
and return the data in real time. I was utterly impressed.
#0 - Roy ( Link) on 2005-01-02 15:39:16 Delete Comment
xmlhttprequest not perfect for this.
unfortuntaly xmlhttprequest is not really suitable for complete validation on a public internet site due to incompatibilities / dependance of javascript (and lack of support in some browsers).

xmlhttprequest excel's when using xul - but having to deal IE's crap/unreliable xml and js impementation.

#1 - Alan ( Link) on 2005-01-02 17:15:07 Delete Comment

Add Your Comment

Follow us on