Custom Attributes: HTML Unrealized by Most.

March 10, 2005

Many developers who use client-side script for field validation don’t realize that IE and other newer browsers allow you to put your own custom attributes on any HTML tag.

For instance:

< input type=”text” name=”txtAge” id=”validation1″ value=0 minvalue=21 maxvalue=200 minerr=”you’re too young to drink” maxerr=”why are you still alive”>

This will allow you to write a generic piece of code that checks value entered against minvalue. If too low, you can display the minerr message. Then check the value against maxvalue, displaying maxerr if the value is too great.  Don’t forget to check for the existence of minvalue.

If (form.validation1.minvalue != null )  {
    if ( form.validation1.value < form.validation1.minvalue) {
         alert(form.validation1.minerr);
         return false;
         }
    }

Using this powerful feature, you could write truly generic form validation code that iterates through every field in a form, validates it, and displays field specific error messages based solely on the field’s attributes.

Now that’s handy!

Next time, I’d like to talk about another commonly unrealized feature: behaviors. While this feature is more geared toward intranet applications — for non-IE browser portability reasons — it’s still very powerful.

Facebooktwittermail