Connecting Tech Pros Worldwide Help | Site Map

Javascript function to make a field mandatory and nonmandatory acording to different

Newbie
 
Join Date: Mar 2008
Posts: 8
#1: Jun 12 '08
I have 2-3 forms.On my first form when i serach to put any word it sends results and after that i click on new button in my search i got different organisation names. on the secong form i passed the organisation name and on this form i want to make email field mandatory and non mandatory according to different organisation names can someone provide me javascript function to solve this problem.


Thanx in Advance
Gaurav
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,126
#2: Jun 12 '08

re: Javascript function to make a field mandatory and nonmandatory acording to different


what have you done so far? let me give you the general idea:

onsubmit of you form2 you have to call a javascript function that checks for the value of the organisation-field and when you need the mail field too then simply check it too.

please post the code you already have ... even the relevant code for this form.

kind regards
Newbie
 
Join Date: Mar 2008
Posts: 8
#3: Jun 24 '08

re: Javascript function to make a field mandatory and nonmandatory acording to different


This is my first form AdminOrgOffsearch.jsp

Expand|Select|Wrap|Line Numbers
  1. <%@ page contentType="text/html; charset=iso-8859-1" %>
  2. <%@ taglib uri="TagLibGeneric.tld" prefix="generic" %>
  3. <%@ taglib uri="TagLibAdminOrgOfficer.tld" prefix="cares" %>
  4. <html>
  5. <head>
  6. <%
  7.     response.setHeader("Cache-Control", "no-cache");
  8.     response.setHeader("Pragma", "no-cache");
  9. %>
  10. <title>EPA - Complaints and Reports of Environmental Significance (CARES)</title>
  11. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  12. <!--include the global styles-->
  13. <%@ include file="/global/GlobalStyles.jsp" %>
  14. <SCRIPT LANGUAGE = "JavaScript">
  15. //include the side bar events
  16. <%@ include file="/global/SideBarEvents.jsp" %>
  17. </SCRIPT>
  18.  
  19. <link rel="stylesheet" href="../stylesheets/epa-cares-style1.css">
  20.  
  21. </head>
  22. <!-- DATA OBJECT -->
  23. <generic:Data objectName="<%=request.getServletPath()%>" searchPage="true" remoteUser="<%=request.getRemoteUser()%>">
  24. <body bgcolor="#FFFFFF" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0" text="#333333" link="#FF9933" vlink="#339966" alink="#339966">
  25. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  26.   <%@ include file="/global/TopBarLinks.jsp" %>
  27.   <tr>
  28.     <td bgcolor="#000099" valign="top"><a href="../main/main_menu.jsp"><img src="../images/interface/epa_logo_pt2.gif" width="150" height="38" alt="Back to Main Menu" border="0"></a></td>
  29.     <td rowspan="2" valign="top">
  30.       <table width="100%" border="0" cellspacing="0" cellpadding="0">
  31.         <tr>
  32.           <td width="5%">&nbsp;</td>
  33.           <td width="95%">&nbsp;</td>
  34.         </tr>
  35.         <tr>
  36.           <td width="5%"><img src="../images/interface/spacer.gif" width="15" height="15"></td>
  37.           <td width="95%" valign="top">
  38.             <p class="main-header">Administration - Organisation Officers</p>
  39.             <form name="form1" method="post" action="AdminOrgOffSearch.jsp">
  40.               <!--populate the row selector-->
  41.               <cares:OrgOfficer rowSelector="true" />
  42.               <cares:OrgOfficer actionFlag="true" />
  43.               <input type="hidden" <cares:OrgOfficer controlName="REDIRECT" />>
  44.               <input type="hidden" name="OFF_ORG_ID" value="<%=request.getParameter("OFF_ORG_ID")%>" >
  45.               <input type="hidden" name="ORG_NAME" value="<%=request.getParameter("ORG_NAME")%>" >
  46.               <table width="90%" border="0" cellspacing="2" cellpadding="3">
  47.                 <tr bgcolor="#E7E3E7">
  48.                   <td width="40%" bgcolor="#E7E3E7">
  49.                     <p align="right">Organisation name: &nbsp;</p>
  50.                   </td>
  51.                   <td width="60%">
  52.                     <p class="blue-highlite"><%=request.getParameter("ORG_NAME")%></p>
  53.                   </td>
  54.                 </tr>
  55.                 <tr bgcolor="#E7E3E7">
  56.                   <td>
  57.                     <p align="right">Officer Surname: &nbsp;</p>
  58.                   </td>
  59.                   <td width="1%">
  60.                     <p>
  61.                       <input type="text" size="35" <cares:OrgOfficer controlName="OFF_SURNAME" />>
  62.                     </p>
  63.                   </td>
  64.                 </tr>
  65.                 <tr bgcolor="#E7E3E7">
  66.                   <td>
  67.                     <p align="right">Officer First Name:&nbsp;&nbsp;</p>
  68.                   </td>
  69.                   <td>
  70.                     <input type="text" size="35" <cares:OrgOfficer controlName="OFF_GIVENNAME" /> >
  71.                   </td>
  72.                 </tr>
  73.                 <tr bgcolor="#E7E3E7">
  74.                   <td valign="middle">
  75.                     <p align="right">Active &nbsp;</p>
  76.                   </td>
  77.                   <td valign="middle">
  78.                     <cares:OrgOfficer controlName="OFF_ACTIVE" comboLoad="YESNO"/>
  79.                   </td>
  80.                 </tr>
  81.    <!--             <tr bgcolor="#E7E3E7">
  82.                   <td valign="middle">
  83.                     <p align="right">Contact &nbsp;</p>
  84.                   </td>
  85.                   <td valign="middle">
  86.                     <cares:OrgOfficer controlName="OFF_KEYCONTACT" comboLoad="YESNO"/>
  87.                   </td>
  88.                 </tr>
  89.     -->
  90.                 <tr bgcolor="#E7E3E7">
  91.                   <td valign="middle">&nbsp;</td>
  92.                   <td height="38" valign="middle" width="1%">
  93.                     <div align="right">
  94.                       <cares:OrgOfficer buttonName="search"/>
  95.                       &nbsp;&nbsp;
  96.                       <input type="submit" name="clear" value="Clear">
  97.                       &nbsp;</div>
  98.                   </td>
  99.                 </tr>
  100.                 <tr bgcolor="#FF9900" valign="middle">
  101.                   <td colspan="2">
  102.                     <div align="right">
  103.                       <cares:OrgOfficer buttonName="new" pageRedirect="AdminOrgOff.jsp"/>
  104.                       &nbsp;&nbsp;&nbsp;
  105.                       <cares:OrgOfficer buttonName="modify" pageRedirect="AdminOrgOff.jsp"/>
  106.                       &nbsp;&nbsp;&nbsp;
  107.                       <cares:OrgOfficer buttonName="view" pageRedirect="AdminOrgOff.jsp"/>
  108.                       &nbsp;&nbsp;&nbsp;
  109.                       <cares:OrgOfficer buttonName="delete" pageRedirect="AdminOrgOff.jsp"/>
  110.                       &nbsp;&nbsp;&nbsp;
  111.                       <cares:OrgOfficer buttonName="Done" pageRedirect="window.location='AdminOrganisationSearch.jsp'"/>
  112.                       &nbsp;</div>
  113.                   </td>
  114.                 </tr>
  115.               </table>
  116.               <br>
  117.               <table width="90%" border="0" cellspacing="2" cellpadding="3">
  118.                 <tr bgcolor="#FF9900">
  119.                   <td width="5%" class="bold-highlite" height="20"><p>&nbsp;</p></td>
  120.                   <td width="33%" class="bold-highlite" height="20">Surname</td>
  121.                   <td width="32%" class="bold-highlite">First Name</td>
  122.                   <td width="10%" class="small">
  123.                     <p class="bold-highlite">Active?</p>
  124.                   </td>
  125.                   <td width="10%" class="small">
  126.                     <p class="bold-highlite">Contact?</p>
  127.                   </td>
  128.                 </tr>
  129.                 <cares:OrgOfficer controlName="TABLE" />
  130.                 <!--
  131.                 TABLE DISPLAY
  132.                 -->
  133.                 <tr bgcolor="#E7E3E7">
  134.                   <td colspan="5">
  135.                     <p align="right">
  136.                     <cares:OrgOfficer buttonName="nav"/>
  137.                     </p>
  138.                   </td>
  139.                 </tr>
  140.               </table>
  141.               <p>&nbsp;</p>
  142.             </form>
  143.             <p class="higlite">&nbsp;</p>
  144.           </td>
  145.         </tr>
  146.       </table>
  147.     </td>
  148.     <td>&nbsp;</td>
  149.   </tr>
  150.   <tr>
  151.     <td bgcolor="#000099" valign="top">
  152.       <%@ include file="/global/SideBarLinksAdmin.jsp" %>
  153.       <br>
  154.       <table width="100%" border="0" cellspacing="0" cellpadding="0">
  155.         <tr>
  156.           <td><img src="../images/interface/brochure_photo.jpg" width="150" height="94" vspace="4" alt="EPA photo - inspecting chemical bottle"></td>
  157.         </tr>
  158.         <tr>
  159.           <td><img src="../images/interface/brochure_text.gif" width="150" height="95" alt="Protecting , restoring and enhancing the environment"></td>
  160.         </tr>
  161.         <tr>
  162.           <td>&nbsp;</td>
  163.         </tr>
  164.       </table>
  165.     </td>
  166.     <td>&nbsp;</td>
  167.   </tr>
  168.   <tr>
  169.     <td bgcolor="#000099" valign="top" align="center">
  170.         <a href="http://www.environment.sa.gov.au/epa/index.html" target="_blank">
  171.           <img src="../images/splash/epa_logo_b.gif" width="66" height="75" alt="Department of Environment and Heritage Logo and Link" border="0">
  172.     </a>
  173.         <br>
  174.         <br>
  175.     </td>
  176.     <td valign="top">&nbsp;</td>
  177.     <td>&nbsp;</td>
  178.   </tr>
  179. </table>
  180. </body>
  181. <cares:OrgOfficer controlName="javascript" />
  182. </generic:Data>
  183. </html>


when i click on new button it opens AdminOrgOff.jsp


Expand|Select|Wrap|Line Numbers
  1. <%@ page contentType="text/html; charset=iso-8859-1" %>
  2. <%@ taglib uri="TagLibGeneric.tld" prefix="generic" %>
  3. <%@ taglib uri="TagLibAdminOrgOfficer.tld" prefix="cares" %>
  4. <html>
  5. <head>
  6. <%
  7.     response.setHeader("Cache-Control", "no-cache");
  8.     response.setHeader("Pragma", "no-cache");
  9. %>
  10. <title>EPA - Complaints and Reports of Environmental Significance (CARES)</title>
  11. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  12. <!--include the global styles-->
  13. <%@ include file="/global/GlobalStyles.jsp" %>
  14. <SCRIPT LANGUAGE = "JavaScript">
  15. //include the side bar events
  16. <%@ include file="/global/SideBarEvents.jsp" %>
  17.  
  18.  /*function onPageSubmit(){ 
  19.     var eMailRequired=document.getElementById("ORG_EMAIL_REQD").value;
  20.     var officeEmail=document.getElementById("OFF_Email").value;
  21.     //var org_name=document.getElementById("ORG_NAME").value;
  22.     if(eMailRequired == 'Y' && officeEmail == ""){ 
  23.         alert("Officer Email is Required");
  24.      return false;
  25.     }
  26.     if(eMailRequired=='Y' && officeEmail!= ""){
  27.     document.form1.action = '../servlet/controlservlet'; 
  28.     window.location = document.form1.REDIRECT.value; 
  29.     document.form1.submit(); 
  30.   } 
  31.  } 
  32.  function onPageCancel(){ 
  33.    var ans = confirm('Are you sure that you want to cancel?') 
  34.    if (ans==true){ 
  35.      window.location = document.form1.REDIRECT.value; 
  36.    } 
  37.  }  */
  38.  </SCRIPT>
  39. <link rel="stylesheet" href="../stylesheets/epa-cares-style1.css">
  40.  
  41. </head>
  42.  
  43. <body bgcolor="#FFFFFF" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0" text="#333333" link="#FF9933" vlink="#339966" alink="#339966"/>
  44. <!-- DATA OBJECT -->
  45.  
  46. <generic:Data objectName="<%=request.getServletPath()%>" remoteUser="<%=request.getRemoteUser()%>">
  47. <!--<cares:OrgOfficer controlName="javascript" />-->
  48. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  49.   <%@ include file="/global/TopBarLinks.jsp" %>
  50.   <tr>
  51.     <td bgcolor="#000099" valign="top"><a href="../main/main_menu.jsp"><img src="../images/interface/epa_logo_pt2.gif" width="150" height="38" alt="Back to Main Menu" border="0"></a></td>
  52.     <td rowspan="2" valign="top">
  53.       <table width="100%" border="0" cellspacing="0" cellpadding="0">
  54.         <tr>
  55.           <td width="5%">&nbsp;</td>
  56.           <td width="95%">&nbsp;</td>
  57.         </tr>
  58.         <tr>
  59.           <td width="5%"><img src="../images/interface/spacer.gif" width="15" height="15"></td>
  60.           <td width="95%" valign="top">
  61.             <p class="main-header">Administration - Organisation Officers</p>
  62.             <form name="form1" method="post" action="">
  63.               <cares:OrgOfficer actionFlag="true" />
  64.               <cares:OrgOfficer pageName="true" />
  65.               <input type="hidden" <cares:OrgOfficer controlName="OFF_ID" />>
  66.               <input type="hidden" <cares:OrgOfficer controlName="REDIRECT" />>
  67.               <input type="hidden" <cares:OrgOfficer controlName="OFF_ORG_ID" />>
  68.               <input type="hidden" name="OFF_LASTUPDATEUSER" value="<cares:OrgOfficer controlName="SECURITY_OFF_USER" />">
  69.               <table width="90%" border="0" cellspacing="2" cellpadding="3">
  70.                 <tr bgcolor="#E7E3E7">
  71.                   <td width="40%">
  72.                     <p align="right">Organisation name: &nbsp;</p>
  73.                   </td>
  74.                   <td width="60%">
  75.  
  76.                       <p class="blue-highlite"><cares:OrgOfficer controlName="ORG_NAME" />
  77.                     </p>
  78.                   </td>
  79.                 </tr>
  80.                 <tr bgcolor="#E7E3E7">
  81.                   <td>
  82.                     <p align="right">Officer Surname: &nbsp;</p>
  83.                   </td>
  84.                   <td width="1%" bgcolor="#E7E3E7">
  85.                     <p>
  86.                       <input type="text" size="35" <cares:OrgOfficer controlName="OFF_SURNAME" />>
  87.                     </p>
  88.                   </td>
  89.                 </tr>
  90.                 <tr bgcolor="#E7E3E7">
  91.                   <td>
  92.                     <p align="right">Officer First Name:&nbsp;&nbsp;</p>
  93.                   </td>
  94.                   <td width="1%">
  95.                     <input type="text" size="35" <cares:OrgOfficer controlName="OFF_GIVENNAME" />>
  96.                   </td>
  97.                 </tr>
  98.                 <tr bgcolor="#E7E3E7">
  99.                   <td valign="middle">
  100.                     <p align="right">Officer E-mail:&nbsp;</p>
  101.                   </td>
  102.                   <td height="38" valign="middle" width="1%">
  103.                     <input type="text" size="35" <cares:OrgOfficer controlName="OFF_EMAIL" />>
  104.                   </td>
  105.                 </tr>
  106.                 <tr bgcolor="#E7E3E7">
  107.                   <td valign="middle">
  108.                     <p align="right">Officer User Name:&nbsp;</p>
  109.                   </td>
  110.                   <td height="38" valign="middle" width="1%">
  111.                     <input type="text" size="35" <cares:OrgOfficer controlName="OFF_USERNAME" />>
  112.                   </td>
  113.                 </tr>
  114.                 <tr bgcolor="#E7E3E7">
  115.                   <td valign="middle">
  116.                     <p align="right">Key Contact &nbsp;</p>
  117.                   </td>
  118.                   <td height="38" valign="middle" width="1%">
  119.                     <cares:OrgOfficer controlName="OFF_KEYCONTACT" comboLoad="YESNO"/>
  120.                   </td>
  121.                 </tr>
  122.                 <tr bgcolor="#E7E3E7">
  123.                   <td valign="middle">
  124.                     <p align="right">Active &nbsp;</p>
  125.                   </td>
  126.                   <td height="38" valign="middle" width="1%">
  127.                     <cares:OrgOfficer controlName="OFF_ACTIVE" comboLoad="YESNO"/>
  128.                   </td>
  129.                 </tr>
  130.  
  131.                 <!--<tr bgcolor="#E7E3E7">
  132.                   <td valign="middle">
  133.                     <p align="right">e-Mail Required:&nbsp;</p>
  134.                   </td>
  135.                   <td height="38" valign="middle" width="1%">
  136.                     <input type="text" size="35" <cares:OrgOfficer controlName="ORG_EMAIL_REQD" />>
  137.                   </td>
  138.                 </tr>-->
  139.         <tr bgcolor="#FF9900" valign="middle">
  140.                   <td colspan="2">
  141.                     <div align="right">
  142.                       <cares:OrgOfficer buttonName="save_buttons" />
  143.                       </div>
  144.                   </td>
  145.                 </tr>
  146.  
  147.  
  148.  
  149.             <!-- <tr bgcolor="#FF9900" valign="middle">
  150.                   <td colspan="2">
  151.                     <div align="right">
  152.                         <input type="submit" name="button_save" value="Save" onclick="javascript:onPageSubmit()">
  153.                         <input type="button" name="button_cancel" value="Cancel" onclick="javascript:onPageCancel()">
  154.                   </div>
  155.                   </td>
  156.                 </tr>-->
  157.  
  158.               </table>
  159.               <br>
  160.               <p>&nbsp;</p>
  161.             </form>
  162.             <p class="higlite">&nbsp;</p>
  163.           </td>
  164.         </tr>
  165.       </table>
  166.     </td>
  167.     <td>&nbsp;</td>
  168.   </tr>
  169.   <tr>
  170.     <td bgcolor="#000099" valign="top">
  171.       <%@ include file="/global/SideBarLinksAdmin.jsp" %>
  172.       <br>
  173.       <table width="100%" border="0" cellspacing="0" cellpadding="0">
  174.         <tr>
  175.           <td><img src="../images/interface/brochure_photo.jpg" width="150" height="94" vspace="4" alt="EPA photo - inspecting chemical bottle"></td>
  176.         </tr>
  177.         <tr>
  178.           <td><img src="../images/interface/brochure_text.gif" width="150" height="95" alt="Protecting , restoring and enhancing the environment"></td>
  179.         </tr>
  180.         <tr>
  181.           <td>&nbsp;</td>
  182.         </tr>
  183.       </table>
  184.     </td>
  185.     <td>&nbsp;</td>
  186.   </tr>
  187.   <tr>
  188.     <td bgcolor="#000099" valign="top" align="center">
  189.         <a href="http://www.environment.sa.gov.au/epa/index.html" target="_blank">
  190.           <img src="../images/splash/epa_logo_b.gif" width="66" height="75" alt="Department of Environment and Heritage Logo and Link" border="0">
  191.     </a>
  192.         <br>
  193.         <br>
  194.     </td>
  195.     <td valign="top">&nbsp;</td>
  196.     <td>&nbsp;</td>
  197.   </tr>
  198. </table>
  199. </body>
  200. <cares:OrgOfficer controlName="javascript" />
  201. </generic:Data>
  202. </html>
on this form officer email text field should be mandatory or non mandatory according to passing organisation name from previous page. i want to make it nonmandatory only when i pass organisation=link communication. this is one of my organisation list.

Please reply soon.


Thanx in Advance
Gaurav
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Jun 24 '08

re: Javascript function to make a field mandatory and nonmandatory acording to different


Tip for next time: when posting code for client-side problems/questions, post the generated client-side code, not your JSP code. It makes deciphering the problem a lot easier.
Quote:

Originally Posted by Gaurav Jhamb

i want to make it nonmandatory only when i pass organisation=link communication. this is one of my organisation list.

I'm not sure exactly what you mean here. Do you mean the organisation name is passed in the URL? The general idea is simple enough. If the organisation name is not passed, then test for email, otherwise there's no need to test, thus making it non-mandatory. For example:
Expand|Select|Wrap|Line Numbers
  1. // assuming org is reference to passed organisation if exists
  2. if (!org) {
  3.  // test for email...
  4. }
To give something more specific, you'll have to give more details.
Reply