?
Solved

Required fields

Posted on 2005-03-23
3
Medium Priority
?
305 Views
Last Modified: 2010-04-01
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
Comment
Question by:pikapi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
vijaydogra earned 200 total points
ID: 13667862
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Check out the latest tech news, community articles, and expert highlights in August's newsletter.
The Summer 2017 Scholarship Winners have been announced!
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

752 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