Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

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}$/);
            }
0
pikapi
Asked:
pikapi
1 Solution
 
vijaydograCommented:
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now