• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retrieving an element when we only know what the id starts with

 
Ranch Hand
Posts: 96
Scala VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

My problem is that I have to write client-side validation for a web-page that has different fields based on several factors. I have some predefined validator methods like:


What I'd like to do is add information to the element itself that would allow my validate() method to validate only those fields that need validation AND call the correct validate method.

I'm not opposed to pushing the correct method and params into an HTML element and parsing it in the function. I'm just unsure of where to put it. I was thinking my id could be something like:



In order to get my array of validators, I need to get the element by what it starts with (i.e. "validate). Is there a way to do those?

Any help and/or suggestions of a different approach is appreciated.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're going to pervert the id to be a list of validation directives? The word that comes to mind is "minefield". Using the id attrinute for anything other than ids is not the most stellar of ideas. (Mine #1: what if two fields need the exact same validation?)

If you want to tack information onto an element, why not do things in a more generally accepted fashion?

For example, in the onload handler:


[ July 13, 2006: Message edited by: Bear Bibeault ]
 
Samuel Cox
Ranch Hand
Posts: 96
Scala VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It certainly is a perversion, but one I am considering. I (mistakenly?) assumed that it would return an array if there was more than one with the same id. Also, I asked for other suggestions to see how inappropriate it seemed;)

Anyhow, the technique that you describe is what I am currently doing. The problem is that the validation is for a very dynamic page that has different elements based on run-time variables. So it certainly have a validate method that dynamically determined which fields needed to be validated and which validation method needed to be called.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ids is a document have to be unique, they can not be the smae for multiple elements. See the spec: http://www.w3.org/TR/REC-html40/struct/global.html#h-7.5.2

Eric
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eric Pascarello:
ids is a document have to be unique, they can not be the smae for multiple elements. See the spec: http://www.w3.org/TR/REC-html40/struct/global.html#h-7.5.2

Eric



Ok, I am probably going to get yelled for this, but just to be obnixious and even though the spec says you can't, you can. It just depends on what you are needing to do. For example:



(Note the space in on load for posting should be removed)
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do a lot of things that you shouldn't do.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Samuel Cox:
The problem is that the validation is for a very dynamic page that has different elements based on run-time variables.



I don't really see any problem. Script is just as easy to genreate dynamically as anything else.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gregg,

You are bad for coding something like that! :roll:

Eric
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea yea, I only did it because Eric said:

ids is a document have to be unique, they can not be the smae for multiple elements



Now, even though Eric can't spell, we all know what he meant. Here is the rough translation:

Element ID's in a document should be unique. The specification defines this however browsers generally don't follow the spec 100%. It's always a good idea to at least follow the spec in your code.



[ducking for cover]
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think document.getElementById in IE may bring back an array. I know that document.all would do that. To lazy to check since I do not break rules, unless they help me.

LOL

Eric
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
document.getElementById("...") will not return an arra, it only returns the first element matching the id. Ofcourse, document.all("...") will give the array.
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:

For example, in the onload handler:



[ July 13, 2006: Message edited by: Bear Bibeault ]



or you can use custom attribute in your input tag, and then access it using getAttribute method.

then you can use

in order to access the value.
 
Samuel Cox
Ranch Hand
Posts: 96
Scala VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yuriy,

That would be perfect if somehow I could get an array back of those elements that have the validate attribute. I'll look into this.
 
Samuel Cox
Ranch Hand
Posts: 96
Scala VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Custom attribute support appears to be unreliable. Ah well.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
var elem = document.formName.elements;
for(var i = 0; i<elem.length; i++){
alert(elem[i].validation);
}

Eric
 
Samuel Cox
Ranch Hand
Posts: 96
Scala VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric,

That certainly would do it. I wanted to avoid iterating over all the elements because we can have 200-300 elements per form with only 40-50 needing client-side validation. I'm guessing that wouldn't be too big of a performance hit though.

I'm more concerned about browser support of custom attributes. I only have so long to research any given topic, and saw differing opinions on whether or not you should use them in a standard approach.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say write the things to an array if they been changed. Than loop through the array or do the validation as the user enters and leaves a field.

Eric
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you put some fake empty tag before all elements, that require validation i.e.

then you can use document.getElementsByTagName('fakename') to get an array of those, and guess what nextSibling property each of them will have
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic