Making my own attibutes for HTML Tags

R

RIck Measham

Is there some way to access an attribute to a tag that I've made up? Eg:
<INPUT type="text" name="phone" size=12 validate="area_phone_au">

I can get the type using:
document.formname.phone.type

But I don't seem to be able to access the 'validate' attribute:
document.formname.phone.validate

Anyone know if there's a way? If there's not, is there some other simple way
to specify validation information for a field? I could do something like:

<INPUT type="text" name="phone" size=12>
<SCRIPT> add_validation(document.formname.phone, "area_phone_au") </SCRIPT>

<INPUT type="text" name="state" size=4>
<SCRIPT> add_validation(document.formname.state, "format:AAA?") </SCRIPT>

The 'validate' function would then store the reference to the field and the
validation information. On submit, we validate all the fields by searching
all the references to see if they're in the form we want. (Have to work
that one out too!)

I'd prefer not to have to add script elements to do the validation. I'd
prefer to be able to store it in an attibute to the field. Of course I
could add it to an eventhandler for the field such as:
<INPUT type="text" name="state" size=4 onFocus="add_validation(this,
"format:AAA?")">

But that's still messier than I'd like it. Of course, all JS cases,
add_validation would have to check to see if it already knew about the
field!

Thanks for your help.
Cheers!
Rick
 
L

Lasse Reichstein Nielsen

RIck Measham said:
Is there some way to access an attribute to a tag that I've made up? Eg:
<INPUT type="text" name="phone" size=12 validate="area_phone_au">

This is not valid HTML, so you shouldn't rely on it working.
I can get the type using:
document.formname.phone.type

But I don't seem to be able to access the 'validate' attribute:
document.formname.phone.validate

The DOM element node is not the same as the HTML tag. They are related,
but there doesn't have to be a strict correspondance.

What should work, if the illegal attribute isn't simply ignored, is:
document.forms['formname'].elements['phone'].getAttribute('validate');
<INPUT type="text" name="phone" size=12>
<SCRIPT> add_validation(document.formname.phone, "area_phone_au") </SCRIPT>

If you do it like this, you could just build one big array of pairs of
elements and their validation constraint. No need to put the data on
the element.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top