1. 01. Incoming
  2. 02. Hifi
  3. 03. Lofi
  4. 04. The Lab
  5. 05. Tags

frequency decoder

Yet Another Unobtrusive Form Validation Script

Posted Tuesday February 28, 2006

frequency decoder

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.

Tags: form, javascript, regexp, unobtrusive, validation

Previous Comments ~

jazzle · http://blog.jazzle.co.uk
#1 · Thursday March 30, 2006

Best implementation I’ve seen!
You need more publicity though – blow that trumpet!

frequency decoder
#2 · Friday March 31, 2006

@ 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

jazzle · http://blog.jazzle.co.uk
#3 · Monday April 3, 2006

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?

frequency decoder
#4 · Monday April 3, 2006

@ 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

jazzle · http://blog.jazzle.co.uk/
#5 · Wednesday May 24, 2006

I discovered a problem – a non-valid but optional input will not prevent the form submission. (i.e. will ‘pass’ validation)

frequency decoder
#6 · Wednesday May 24, 2006

@ 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.

bas
#7 · Monday July 17, 2006

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.

frequency decoder
#8 · Monday July 17, 2006

@ 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.

Chris Grafix · http://www.dotcomerica.com,
#9 · Wednesday July 19, 2006

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

frequency decoder
#10 · Wednesday July 19, 2006

@ 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

Fransjo Leihitu · http://www.leihitu.nl
#11 · Thursday October 5, 2006

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.

Frequency Decoder
#12 · Thursday October 5, 2006

@ 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.

jojje · http://www.ferrypin.net
#13 · Wednesday November 15, 2006

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..

Frequency Decoder
#14 · Wednesday November 15, 2006

@ 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.

jojje · http://www.ferrypin.net
#15 · Wednesday November 15, 2006

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. :)

Frequency Decoder
#16 · Wednesday November 15, 2006

@ 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:

  
  // Only check the two fields are identical when the user presses submit
  if(onsubmit) {
    var p1 = document.getElementById('pass1');
    var p2 = document.getElementById('pass2');

    var p1val = p1.value;
    var p2val = p2.value;

    if(!p1val || !p2val || (p1val != p2val)) {
      // We have an error

      // Do your error alert (or similar) here
      // alert(‘The password confirmation does not match the password’);

      // Return false so the form does not submit
      return false;
  }

  // Return true as were validating individual fields
  return true;        
  
  

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.

Greg
#17 · Thursday March 1, 2007

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 :(

Flesher · http://www.vetshelpcenter.com/
#18 · Saturday March 10, 2007

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

Frequency Decoder
#19 · Monday March 12, 2007

@ 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.

Tom
#20 · Tuesday March 20, 2007

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.

Daniel
#21 · Monday March 26, 2007

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!

Frequency Decoder
#22 · Monday March 26, 2007

@ 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.

Popular Frequencies

  • Unobtrusive Table Sort Script (revi…
    Saturday September 16, 2006
  • Unobtrusive Date-Picker Widget Upda…
    Monday October 02, 2006
  • Unobtrusive Table Sort Script
    Friday November 18, 2005
  • Unobtrusive Date-Picker Widgit
    Friday October 14, 2005
  • Unobtrusive Table Actions Script
    Thursday November 15, 2007

Google Ads

All articles