Solved

Form validation - multiple fields with the same name not validating properly

Posted on 2003-11-14
8
307 Views
Last Modified: 2008-02-26
I have a form that generates dynamically depending on what is chosen from several dropdowns. This form sometimes generates only 1 data field (txtQFound) and sometimes up to 10 datafields all having the same name (txtQFound).

My JS form validation is not validating this field, perhaps because most of the time there are multiple? How do I do this? I have this validating 6 other fields and the rest of the fields are validating properly.

Current JS validation code (only the field in question):

function validate() {
    errorFields='';
    errorMessage='';
    vQFound=frmQASort.txtQFound.value;

if (vQFound=='') {
         errorFields+=' Quantity Found\n';
         event.returnValue=false;     }

if (errorFields!='') {
         errorMessage+=' The following fields are required:\n\n';
         errorMessage+=errorFields;
         alert(errorMessage);     }}
0
Comment
Question by:Mustangz
  • 3
  • 3
  • 2
8 Comments
 
LVL 10

Accepted Solution

by:
Bustarooms earned 80 total points
ID: 9748856
function validate() {
   errorFields='';
   errorMessage='';
   vQFound=frmQASort.txtQFound.value;

var theEl = frmQASort.txtQFound;
for(i=0; i < theEl.length; i++){
if (theEl[i].value=='') {
        errorFields+=' Quantity Found\n';
        event.returnValue=false;     }
}

if (errorFields!='') {
        errorMessage+=' The following fields are required:\n\n';
        errorMessage+=errorFields;
        alert(errorMessage);     }}
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9748928
>>>event.returnValue=false;

Will not work for Netscape

use return false instead
0
 

Author Comment

by:Mustangz
ID: 9748998
This is an internal app, and we only use IE.

I'll go try this and let you know what happens.. thanks for the quick response.
0
 

Author Comment

by:Mustangz
ID: 9749024
Works like a charm... thank you much Bustarooms.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:jaysolomon
ID: 9749037
Ok just as long as everyone uses IE your fine.

Good Job Busta
0
 
LVL 10

Expert Comment

by:Bustarooms
ID: 9749040
actually it doesn't

if there is only one field than it will not work cause a problem

give me a sec and ill post a fix
0
 
LVL 10

Expert Comment

by:Bustarooms
ID: 9749060
function validate() {
   errorFields='';
   errorMessage='';
   vQFound=frmQASort.txtQFound.value;

  var theEl = frmQASort.txtQFound;
  if(theEl.length){
      for(i=0; i < theEl.length; i++){
         if (theEl[i].value=='') {
           errorFields+=' Quantity Found\n';
           event.returnValue=false;     }
      }
   }
   else{
       if (theEl.value=='') {
         errorFields+=' Quantity Found\n';
         event.returnValue=false;     }
   }

if (errorFields!='') {
        errorMessage+=' The following fields are required:\n\n';
        errorMessage+=errorFields;
        alert(errorMessage);     }
}
0
 

Author Comment

by:Mustangz
ID: 9749139
Of course you were right.. I tried it with just one and it didn't validate, but then used your update fix and it does work no matter the number of input fields..

I appreciate you being so thorough, and posting the fix.

thanks again!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery detect if it is a mobile device 3 107
Allow space in this pattern 2 48
Poster Video HTML 5 Scale to fit video 3 38
How can I get this SlideToggle to behave? 6 18
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now