The Lowdown
I won’t go into very much detail as the script is still in an alpha stage of development…
At a glance:
- Unobtrusive javascript (an obligation in these heady days of web-two-oh)
- Validates multiple forms per page
- Multiple validation rules can be stipulated for each form field
- Leverages the power of Regular Expressions
- An additional callBack function can be defined within the form tags class attribute
- Validation rules are stipulated within harmless HTML comments, removing the need for adding invalid attributes to the DOM or extra in-line classnames to the form fields themselves (Caveat: it does mean therefore that comments are added to the HTML markup)
- Both input fields and their associated labels targeted, giving the developer two opportunities to visually indicate erroneous input to the user
- Both warnings and errors thrown – warnings before the submit button has been pressed and errors after
- Individual fields can be validated onchange or onblur (hint: for accessibility reasons, always try to use the onchange event)
- Works with both implicit and explicit labels
Note: The script will quietly fail Internet Explorer 5.x as it fails to add HTML comments to the DOM. Also, should you require a script that validates all types of form elements, you should check out Cody’s Live JavaScript Validation.
Tested in Internet Explorer 6, Mozilla and Firefox.
View the form validator demo.

Previous Comments ~
Best implementation I’ve seen!
You need more publicity though – blow that trumpet!
@ jazzle: Thanks for the kind words… always nice to hear!
I’ve actually not looked at the script since the day it was uploaded so might try to work on it this weekend (Safari 2.0.3? has problems with something – which I would like to fix).
It’s just a shame IE5.X doesn’t play fair – then again, IE5.X is fast approaching the realm of “unsupported browsers” for many web applications…
Regards,
Brian
I’m having difficulty persuading the script to work when the inputs are placed within a table.
Am I missing something simple, or is this a known problem?
@ jazzle: Is the script throwing errors?
If you mail me a copy of your HTML I shall have a look at it tomorrow.
Update: The problem was that the form tag was nested within the table tag, moving the form tag above the table tag fixed the problem…
Thanks,
Brian
I discovered a problem – a non-valid but optional input will not prevent the form submission. (i.e. will ‘pass’ validation)
@ Jazzle: Hi Jazzle, in fact, that’s supposed to happen. Optional inputs do not stop the form being submitted even if they are invalid. Required inputs will stop the form submission if they contain invalid data.
I should have made this a bit clearer!
Regards,
Brian.
Looks really great. Have been looking around. Works really good and clear for end-users. So thanks very much for sharing.
Just one question. If yo upress submit – then it shows all the errors. But if you immediately plress submit again, it will submit the form anyway.
@ bas: Hi bas, I can’t seem to replicate the problem (using the demo). what browser/platform combo are you using?
Thanks for the comment,
Brian.
Hi,
I think your sript is one of the easiest to use out there. Great job! I tried out your script but had problems because of a undefined ‘Effect’ function:
new Effect.Opacity($(‘err_’+elem.id), {to:0.9, duration:2});
It must have to do because you have the effect declared in an include .js file
effects.js. I wonder if you could post the code of the effects.js file or email it to me. I sure would appreciate it greatly.
Keep it up,
Thanx,
Chris
@ Chris: Hi Chris, the actual “Effect” is only used within the demo (the callback function “demoCallBack”) and isn’t actually required by the core validation script.
The effect is actually part of the script.aculo.us JavaScript library and can be downloaded from the script.aculo.us site.
Thanks for the comment,
Brian
what will happen if you disable javascript? I assume the form just submits. Is there a way to tell the server side wich fields are required? If you can define it in the form and the user submits (without javascript), you can make a generic validation script on the server side.
@ Fransjo: Hi Fransjo, like all JavaScript form validation scripts, this one just won’t work when JavaScript is disabled.
You should never rely on JavaScript being enabled and should always double check the form server side. This script is meant to compliment the server-side validation.
You could use a hidden input field to tell the server which fields are required (with it’s value attribute populated with a comma seperated list of required input names and regular expressions perhaps) .
Thanks for the comment,
Brian.
Hi! Great script! and easy to manage too! I just have one question: Is it possible to embed a check for confirmation fields. So I can check if value in password field is same with confirm password field.
I suspect that regexps cant do that, so perhaps there is another way to do this..
@ jojje: Hi jojje, you can use the callback functionality to check the two field values are identical, if their not, return false from the function to stop the form submitting. Hope this helps.
Regards,
Brian.
Thanks a million for your fast reply! You helped to point me in the right direction, but I’m afraid my javascript skills aren’t enough to crack this one.
Actually I had removed the callback function cause I didn’t want the list with errors to appear. But I now understand that it can be used for other purposes as well, so I put it right back where it was..
The function to check wether two field values are identical is tiny, but I can’t integrate it within the callback function.
Is there any chance we can communicate over mail or IM? Promise I will not take up too much of your time. :)
@ JOJJE: Hi Jojje, assuming your two input's have the id's "pass1" and "pass2", something like this should suffice as the contents to the callback function:
You can email me at brian (at) modernartcafe [d0t] net should you need any more info (I'm slightly pressed for time so the answer might not be immediate).
Hope this helps,
Brian.
Cool script. Sadly, I’m not sure if Safari is able to read comment nodes. This code:
javascript:var el=document.createElement(‘div’);el.innerHTML=’texttext’;alert(el.childNodes2.nextSibling);
Gives “null”. Comment nodes aren’t very well documented online unfortunately :(
Putting parameters in comments is a bad idea for the simple reason of cut and paste; the comments don’t follow; and I like everything to be inside the tags; the class is still the best place. Maybe you could have both as an option if you really prefer comments but I think this script would be better if you took this into consideration. Other than that I like the idea.
Thanks
@ Greg: Hi Greg, yep, theres little to no info available about comments in the DOM. I know for sure that IE 5/5.5 don’t even add them to the DOM – it’s a pity Safari does the same.
@Flesher: Hi Flesher. You can’t pass regular expressions in the class attribute as they contain characters that are illegal to use in a className (the ^ character for example). This was the entire point of using comments to store the regular expressions. The script could be rewritten to search for comment nodes after the form input’s and not before – this might alleviate some of the cut & paste worries.
Regards,
Brian.
I like it a lot. I noticed the same problem that Bas reported when I removed prototype.js and/or effects.js from the demo. I got the impression from these comments that those were optional. Everything else still seemed to work. I don’t know enough about those includes to explain why. I’m using Firefox 2.0.0.2 on XP SP2.
Yes, very cool script!!
Just wondering if anyone has an update on the “Safari browser” problem? I’m sorry but my javascript skills are not as profound as you guys… :( But at least I got it to work and do what I wanted it to do, but thanks so much for your hard work!
@ Tom & Daniel: Hi guys, remember, this is an experiment and was never intended to be used within production environments!
In fact, I have an improved version somewhere on my harddrive that combines all of the validation rules into a single JSON block hidden within a single comment node.
As for Safari, there’s no way the script will work until the Safari team integrate comment node suppport into their DOM parser.
Another problem is that comments within the document invalidate your page if they contain anything but comments (ok, the page will pass “machine” validation but not “human” validation). As we are using the comment nodes(s) to store data, it (like IE conditional comments) breaks the W3C comment rule and therefore breaks (human) validation.
I’ll try to post a copy of the new (unfinished) code sometime this week. Feel free to improve it (or suggest improvments).
Sorry I can’t be more help,
Brian.
Comments are currently closed for this article but feel free to email me with your input - I’d love to hear it.