Solved

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

Posted on 2003-11-14
8
306 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
Comment Utility
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
Comment Utility
>>>event.returnValue=false;

Will not work for Netscape

use return false instead
0
 

Author Comment

by:Mustangz
Comment Utility
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
Comment Utility
Works like a charm... thank you much Bustarooms.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

by:jaysolomon
Comment Utility
Ok just as long as everyone uses IE your fine.

Good Job Busta
0
 
LVL 10

Expert Comment

by:Bustarooms
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

744 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

15 Experts available now in Live!

Get 1:1 Help Now