Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic form validation with Javascript? 16

Status
Not open for further replies.

gbaughma

IS-IT--Management
Staff member
Joined
Nov 21, 2003
Messages
4,773
Location
US
Here's the situation.

I have form web page that is dynamically built from ASP. In other words, the record number is included with the name of the form field, so I could have frm23depart and frm27depart, for example.

I want to do some validation on those fields, but what I need is something like:

function compare(fieldno){
var1=document.form1.frm<fieldno>depart.value;
....

See the issue? I need the javascript to be dynamically passed which FIELD it needs to use for it's calculations

I'm using onchange="compare(<%=fieldno%>)" ... to pass the field number to the javascript.

Any thoughts on this? It's holding up a project that I'm working on.....

I don't really want to dynamically generate multiple functions just for this (although that would be an option).

--Greg


Just my $0.02

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
Here's another way to do it that might help you:

var1 = eval("document.form1.frm" + fieldno + "depart.value");

The "eval" tag is definitely your friend in a case like this. :)

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
eval should be avoided if possible. It REALLY slows the code execution down, and the other methods shown are far superior.

Lee
 
there you go :-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I haven't seen a problem with it slowing down code execution, but perhaps this depends upon the size of the form. However, I'm currently using it with something around the size of 100+ checkboxes on a page (ugly, yes I know), and I'm not having a problem.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
lol, 100+ checkboxes, does anyone actualy fill them all in?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Actually, I have a form I wrote for work which has about 75 checkboxes... all grouped together. It's for incident reporting...

THAT was a fun one to process, especially considering there was validation and error-checking built in as well.



Just my $0.02

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
speaking of validation & error checking, would it be standard practice to have server side AND client side validation, just for those pesky surfers without JS enabled?

is it worth doing client side if you still have to do server side anyway?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
is it worth doing client side if you still have to do server side anyway?
Yes definately. If you can save the user the time of going back to the server to get their details validated (and at the same time save the demand on the server) then go for it. As for those "pesky users" they may not have the choice of having JavaScript enabled (i.e. those with special needs and are using things such a Text Readers etc).



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 

1DMF,

Client side validation is done to improve user experience (more user friendly), and reduce traffic roundtrips between client and server.

Server side validation is done to ensure security - never ever trust input from an external source - especially where a user is concerned. It also has the bonus of catching any users that disable JS.



A smile is worth a thousand kind words. So smile, it's easy! :-)
 
I thought as much, I must admit, I have tended to rely more on client side over the last few years, for the obvious advantages it gives, i should probably tighten up on the server side validation.

Although I still see Security & Validation as two seperate things, and always ensure the server side has security (who are they, are they logged in, should they be doing that) aside from what they supplied and is it valid.

I guess relying on if a form is filled in correctly purely by JS and not allowing submit until it is, is let down by the less sophisticated browsers.

Those pesky blighters ;-)



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
You know, as I think about validation, one VERY funny scene comes to mind.

Remember the movie "War games"? And they way that they convinced the computer (WOPR) that there was no winning solution, was to have it play tic-tac-toe against itself. And how did they do that? By telling it zero players, so it played against itself.

But when it prompted for the number of players, the person TYPED IN THE WORD ZERO.

Now, this goes to show that (l)users will do the unexpected.... however, I guess hats off to the programmer (can't remember his name) who thought of that, and had good validation in his tic-tac-toe program. :)

I can't even count the number of times I've had to either fix code, or put in better validation, for stupid stuff like that.... I had one form that CLEARLY SAID "Enter the number of minutes...." and if it was 70 minutes, they'd put in 1:10.... puked my program every time. <ROFL> (Yes, I eventually put in a validation code looking for a colon, and if there was one there, did an alert to remind them that an HOUR is 60 minutes, not 1:00... etc.)



Just my $0.02

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
gbaughma said:
did an alert to remind them that an HOUR is 60 minutes, not 1:00
...
Just my $0.02

...and two cents is 2[&cent;], not $0.02! :)


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Yeah... I was too lazy to imbed the ¢ sign. <LOL>

Well, I'm off to fix my sig line! <rofl>



Just my $0.02

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
Yup, people can check in all 100 checkboxes, and there's a ton of logic validation that can either be an "AND" or an "OR" depending on the value of the checkboxes.

One word: recursion.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
Kyrene,

do you have anchors or buttons to 'Check All' or 'Clear All' or 'Reverse Checks'?

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
I wonder why kyrene never got a star??????

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 

You're just jealous Chrissie - do you want one aswell ?

A smile is worth a thousand kind words. So smile, it's easy! :-)
 
I was trying to be not to obvious but since you ask, YES.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
I didn't get one because I'm not worthy! *sniffs*

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top