Required fields

Hi

I've created a form, and I wish to use JSP to ensure that a user fills in all of my required fields... the code that I have at the moment works in IE, but when used in Firefox, it finds the unfilled fields but says they're "undefined" rather than telling me what fields they are (and therefore needs to be filled in)... can someone please help me? And maybe even suggest a better way to code it (since I know this code probably isn't the best or simplest way to do what I need)... Thank you! (see code below)

*********************************************
// JavaScript Document


    var reqfields = new Array();
    var intArrayCount = 0;

    function defineRequired(str)
    {
                  reqfields = reqfields.concat (str.split(',')) ;
//                  displayArray();
    }
      
       function requiredCheck(obj,str)
       {
                  //alert (obj);
                  if (obj.checked) {
                        //alert (obj+" is checked") ;
                        defineRequired(str) ;
                  }else{
                        //alert (obj+" is unchecked");
                        scanArray(str) ;
                  }
            }
                  
      function scanArray(str) {
            //scans array & find position
            var tempArray = new Array() ;
            tempArray = str.split(',') ;
            //alert ("Temp Array Length "+tempArray.length);
            for(k=0;k<tempArray.length;k++) {
                  for(i=0;i<reqfields.length;i++) {
                        //alert ("looking for "+tempArray[k]+" in ReqFields ["+i+"]: "+ reqfields[i]);
                        
                        if( reqfields[i] == tempArray[k] ) {
                              //alert("Found "+tempArray[k]);
                              document.form[tempArray[k]].value = "";
                              removeRequired (i,tempArray[k]) ;
                              
                        }
                  }
            }
      }
      
      function displayArray(){
            document.form.outputbox.value = reqfields ;
      }


      function removeRequired (pos,str)
      {
            reqfields.splice(pos,1);
            //alert ("Removed "+str+" from "+pos);
            //displayArray();
            
      }

                        
                        
            function checkform(of)
            {
                  var em,i,f,ty;
                  var labels,fieldnames;
                  fieldnames=new Array();
                  if(document.getElementsByTagName('label').length>0)
                  {
                        labels=document.getElementsByTagName('label');
                        for(i=0;i<labels.length;i++)
                        {
                              fieldnames[labels[i].getAttribute('htmlFor')]=labels[i].firstChild.nodeValue;
                        }
                  }
                  if(document.getElementById('required'))
                  {
                        // reqfields=document.getElementById('required').value.split(',');
                        if(document.getElementById('errormsg')){
                              em=document.getElementById('errormsg');
                              em.parentNode.removeChild(em);
                        }
                        for(i=0;i<reqfields.length;i++)
                        {
                              f=document.getElementById(reqfields[i]);
                              if(f.previousSibling && /img/i.test(f.previousSibling.nodeName)){
                                    f.parentNode.removeChild(f.previousSibling);
                              }
                              f.style.background='transparent';
                              if(f){
                                    ty=f.type.toLowerCase();
                                    switch(ty)
                                    {
                                          case 'text':
                                                if(f.value==''){adderr(f.id,fieldnames[f.id],of)}                                          
                                          break;
                                          case 'textarea':
                                                if(f.value==''){adderr(f.id,fieldnames[f.id],of)}                                          
                                          break;
                                          case 'checkbox':
                                                if(!f.checked){adderr(f.id,fieldnames[f.id],of)}                                          
                                          break;
                                          case 'radio':
                                                if(!f.checked){adderr(f.id,fieldnames[f.id],of)}                                          
                                          break;
                                          /* extend as needed */
                                    }
                              }
                              
                        }
                        
                  }
                  if(document.getElementById('errormsg'))
                  {
                        return false;
                  }
            }
            function adderr(id,name,of)
            {
                   var se,i,nli,na,newli,newa;
                  //i=document.createElement('img');
                  //i.src='alert.gif';
                  //i.alt='Error';
                  //i.title='This field has an error!';
                  se=document.getElementById(id);
                  //se.parentNode.insertBefore(i,se)
                  se.style.background='#fcc';
                  if(!document.getElementById('errormsg')){
                        var em=document.createElement('p');
                        em.id='errormsg';
                        em.appendChild(document.createTextNode('Please enter the following fields: '))
                        ul=document.createElement('ul');
                        ul.id='errorlist';
                        em.appendChild(ul);
                        for(var i=0;i<of.getElementsByTagName('input').length;i++)
                        {
                              if(/submit/i.test(of.getElementsByTagName('input')[i].getAttribute('type')))
                              {
                                    var sb=of.getElementsByTagName('input')[i];
                                    break;
                              }
                        }
                        em.style.border='2px solid #c00';
                        em.style.padding='5px';
                        em.style.width='20em';
                        sb.parentNode.insertBefore(em,sb);
                  }
                  if(id=='email' && !isEmailAddr(document.getElementById(id).value)){
                        pn=document.createElement('p');
                        pn.appendChild(document.createTextNode('Your email seems to be invalid'))      
                        document.getElementById('errormsg').appendChild(pn);
                  }
                  newli=document.createElement('li');
/* Changes from errorlist example */
                  newa=document.createElement('a');
                  newa.appendChild(document.createTextNode(name));
                  newa.href='#'+id;
                  newli.appendChild(newa)
/* Changes end */
                  document.getElementById('errorlist').appendChild(newli);
            }

            function isEmailAddr(str)
            {
                return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
            }
pikapiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vijaydograCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.