Solved

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

Posted on 2003-11-14
8
308 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Mustangz
ID: 9749024
Works like a charm... thank you much Bustarooms.
0
 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

803 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