?
Solved

JavaScript form ID attribute reference problem

Posted on 2003-03-27
4
Medium Priority
?
216 Views
Last Modified: 2013-11-19
Hello I am using the following function to check a form for id values that have the string "req" in them.  If the string exists then I check for content and alert the user if it doesn't exist.

Works great in IE but Netscape will not recognize the ID attribute.  How should I modify the code so it will work in Netscape also.  (it's a nifty function if we can do that)

Thanks,
Daniel

JAVASCRIPT FUNCTION
==================================
//This function checks the required field and makes sure that user input some data in those and it is not just empty spaces
function CheckReqFields(strFormName, strReqParam){
 var errorFields = "";
 var blnReturn = true;
 for (var i=0;i<eval(strFormName).length;i++){
  if (eval(strFormName).elements[i].ID.substr(0, strReqParam.length) == strReqParam){
   var strTemp = Trim(eval(strFormName).elements[i].value);
   if (strTemp == ''){
    blnReturn = false;
     errorFields += eval(strFormName).elements[i].name + "\n";
   }
  }
 }
 
 if (blnReturn){
  eval(strFormName).submit();
 }
 else{
  alert("Please fill out all the required fields before submitting the form.\n\n" + errorFields);
 }
}

//This function takes out all the spaces from a string from both sides and the middle and returns the result.
//It is used in conjunction with the above funtion to make sure the user simply didn't put it spaces in the input text field
function Trim(strInput){
 var strTrimmmed = '';
 for (var i = 0;i<strInput.length; i++){
  if (strInput.charCodeAt(i)!=32){
   strTrimmmed += strInput[i];
  }
 }
 return strTrimmmed;
}

HTML CALLER
==================================
     <form method=post name="registerForm" action="register.jsp">
     <table width="300" cellpadding="1" cellspacing="1">
     <tr>
          <td colspan="2" valign="top" class="descriptiveCopy">
               <jsp:getProperty name="register" property="registerMsg"/>
               <br><br>
          </td>
     </tr>
     <tr>
          <td valign="top" align="right" class="formCopy">
               First Name:
          </td>
          <td valign="top">
               <input type=text name=firstName id=req_firstName size=20 value="<%= register.getFirstName() %>">
          </td>
     </tr>
     <tr>
          <td colspan="2" align="right"><br>
               <input type="button" value="register" onClick="JavaScript:CheckReqFields('registerForm', 'req')">
          </td>
     </tr>
0
Comment
Question by:dminton
  • 3
4 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8222330
This should get you started:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function CheckReqFields(strFormName,strSearchString){
     objForm = document.forms[strFormName]
     for(i=0;i<objForm.length;i++){
          if(objForm.elements[i].id.indexOf(strSearchString)>-1){
               alert(objForm.elements[i].name);
          }
     }
}

//-->
</SCRIPT>


</HEAD>
<BODY>

  <form method=post name="registerForm" action="register.jsp">
    <table width="300" cellpadding="1" cellspacing="1">
    <tr>
         <td colspan="2" valign="top" class="descriptiveCopy">
              <jsp:getProperty name="register" property="registerMsg"/>
              <br><br>
         </td>
    </tr>
    <tr>
         <td valign="top" align="right" class="formCopy">
              First Name:
         </td>
         <td valign="top">
              <input type=text name=firstName id=req_firstName size=20 value="">
         </td>
    </tr>
    <tr>
         <td colspan="2" align="right"><br>
              <input type="button" value="register" onClick="JavaScript:CheckReqFields('registerForm', 'req')">
         </td>
    </tr>
    </table>
</BODY>
</HTML>


Fritz the Blank
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 200 total points
ID: 8222425
Okay, here is the whole thing. I got rid of your trim function and replaced it with something a little simpler. Let me know how this works for you:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function CheckReqFields(strFormName,strSearchString){
     var errorFields = "";
     var blnReturn = true;
    objForm = document.forms[strFormName]
    for(i=0;i<objForm.length;i++){
         if(objForm.elements[i].id.indexOf(strSearchString)>-1){
               if(objForm.elements[i].value.split(" ").join() ==""){
                    blnReturn = false;
                    errorFields += objForm.elements[i].name + "\n";
               }
         }
    }
     if (blnReturn){
          eval(strFormName).submit();
     }else{
          alert("Please fill out all the required fields before submitting the form.\n\n" + errorFields);
     }
}

//-->
</SCRIPT>


</HEAD>
<BODY>

 <form method=post name="registerForm" action="register.jsp">
   <table width="300" cellpadding="1" cellspacing="1">
   <tr>
        <td colspan="2" valign="top" class="descriptiveCopy">
             <jsp:getProperty name="register" property="registerMsg"/>
             <br><br>
        </td>
   </tr>
   <tr>
        <td valign="top" align="right" class="formCopy">
             First Name:
        </td>
        <td valign="top">
             <input type=text name=firstName id=req_firstName size=20 value="">
        </td>
   </tr>
   <tr>
        <td colspan="2" align="right"><br>
             <input type="button" value="register" onClick="JavaScript:CheckReqFields('registerForm', 'req')">
        </td>
   </tr>
   </table>
</BODY>
</HTML>


Fritz the Blank
0
 

Author Comment

by:dminton
ID: 8226272
Great work,

It works very well and I think it is a great function for required fields.

Thanks.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8226455
Glad to have helped.

It's okay, but it only works for text fields. I prefer a different approach that can accomodate all input types.

Fritz the Blank
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

580 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