Link to home
Start Free TrialLog in
Avatar of Mustangz
Mustangz

asked on

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

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);     }}
ASKER CERTIFIED SOLUTION
Avatar of Bustarooms
Bustarooms
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaysolomon
jaysolomon

>>>event.returnValue=false;

Will not work for Netscape

use return false instead
Avatar of Mustangz

ASKER

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.
Works like a charm... thank you much Bustarooms.
Ok just as long as everyone uses IE your fine.

Good Job Busta
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
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);     }
}
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!