?
Solved

JavaScript form ID attribute reference problem

Posted on 2003-03-27
4
Medium Priority
?
206 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
[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
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
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

764 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