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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gaurav Jhamb
    New Member
    • Mar 2008
    • 8

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

    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
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    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

    Comment

    • Gaurav Jhamb
      New Member
      • Mar 2008
      • 8

      #3
      This is my first form AdminOrgOffsear ch.jsp

      [CODE=java]<%@ page contentType="te xt/html; charset=iso-8859-1" %>
      <%@ taglib uri="TagLibGene ric.tld" prefix="generic " %>
      <%@ taglib uri="TagLibAdmi nOrgOfficer.tld " prefix="cares" %>
      <html>
      <head>
      <%
      response.setHea der("Cache-Control", "no-cache");
      response.setHea der("Pragma", "no-cache");
      %>
      <title>EPA - Complaints and Reports of Environmental Significance (CARES)</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <!--include the global styles-->
      <%@ include file="/global/GlobalStyles.js p" %>
      <SCRIPT LANGUAGE = "JavaScript ">
      //include the side bar events
      <%@ include file="/global/SideBarEvents.j sp" %>
      </SCRIPT>

      <link rel="stylesheet " href="../stylesheets/epa-cares-style1.css">

      </head>
      <!-- DATA OBJECT -->
      <generic:Data objectName="<%= request.getServ letPath()%>" searchPage="tru e" remoteUser="<%= request.getRemo teUser()%>">
      <body bgcolor="#FFFFF F" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0 " text="#333333" link="#FF9933" vlink="#339966" alink="#339966" >
      <table width="100%" border="0" cellspacing="0" cellpadding="0" >
      <%@ include file="/global/TopBarLinks.jsp " %>
      <tr>
      <td bgcolor="#00009 9" valign="top"><a href="../main/main_menu.jsp"> <img src="../images/interface/epa_logo_pt2.gi f" width="150" height="38" alt="Back to Main Menu" border="0"></a></td>
      <td rowspan="2" valign="top">
      <table width="100%" border="0" cellspacing="0" cellpadding="0" >
      <tr>
      <td width="5%">&nbs p;</td>
      <td width="95%">&nb sp;</td>
      </tr>
      <tr>
      <td width="5%"><img src="../images/interface/spacer.gif" width="15" height="15"></td>
      <td width="95%" valign="top">
      <p class="main-header">Adminis tration - Organisation Officers</p>
      <form name="form1" method="post" action="AdminOr gOffSearch.jsp" >
      <!--populate the row selector-->
      <cares:OrgOffic er rowSelector="tr ue" />
      <cares:OrgOffic er actionFlag="tru e" />
      <input type="hidden" <cares:OrgOffic er controlName="RE DIRECT" />>
      <input type="hidden" name="OFF_ORG_I D" value="<%=reque st.getParameter ("OFF_ORG_ID")% >" >
      <input type="hidden" name="ORG_NAME" value="<%=reque st.getParameter ("ORG_NAME") %>" >
      <table width="90%" border="0" cellspacing="2" cellpadding="3" >
      <tr bgcolor="#E7E3E 7">
      <td width="40%" bgcolor="#E7E3E 7">
      <p align="right">O rganisation name: &nbsp;</p>
      </td>
      <td width="60%">
      <p class="blue-highlite"><%=re quest.getParame ter("ORG_NAME") %></p>
      </td>
      </tr>
      <tr bgcolor="#E7E3E 7">
      <td>
      <p align="right">O fficer Surname: &nbsp;</p>
      </td>
      <td width="1%">
      <p>
      <input type="text" size="35" <cares:OrgOffic er controlName="OF F_SURNAME" />>
      </p>
      </td>
      </tr>
      <tr bgcolor="#E7E3E 7">
      <td>
      <p align="right">O fficer First Name:&nbsp;&nbs p;</p>
      </td>
      <td>
      <input type="text" size="35" <cares:OrgOffic er controlName="OF F_GIVENNAME" /> >
      </td>
      </tr>
      <tr bgcolor="#E7E3E 7">
      <td valign="middle" >
      <p align="right">A ctive &nbsp;</p>
      </td>
      <td valign="middle" >
      <cares:OrgOffic er controlName="OF F_ACTIVE" comboLoad="YESN O"/>
      </td>
      </tr>
      <!-- <tr bgcolor="#E7E3E 7">
      <td valign="middle" >
      <p align="right">C ontact &nbsp;</p>
      </td>
      <td valign="middle" >
      <cares:OrgOffic er controlName="OF F_KEYCONTACT" comboLoad="YESN O"/>
      </td>
      </tr>
      -->
      <tr bgcolor="#E7E3E 7">
      <td valign="middle" >&nbsp;</td>
      <td height="38" valign="middle" width="1%">
      <div align="right">
      <cares:OrgOffic er buttonName="sea rch"/>
      &nbsp;&nbsp;
      <input type="submit" name="clear" value="Clear">
      &nbsp;</div>
      </td>
      </tr>
      <tr bgcolor="#FF990 0" valign="middle" >
      <td colspan="2">
      <div align="right">
      <cares:OrgOffic er buttonName="new " pageRedirect="A dminOrgOff.jsp"/>
      &nbsp;&nbsp;&nb sp;
      <cares:OrgOffic er buttonName="mod ify" pageRedirect="A dminOrgOff.jsp"/>
      &nbsp;&nbsp;&nb sp;
      <cares:OrgOffic er buttonName="vie w" pageRedirect="A dminOrgOff.jsp"/>
      &nbsp;&nbsp;&nb sp;
      <cares:OrgOffic er buttonName="del ete" pageRedirect="A dminOrgOff.jsp"/>
      &nbsp;&nbsp;&nb sp;
      <cares:OrgOffic er buttonName="Don e" pageRedirect="w indow.location= 'AdminOrganisat ionSearch.jsp'"/>
      &nbsp;</div>
      </td>
      </tr>
      </table>
      <br>
      <table width="90%" border="0" cellspacing="2" cellpadding="3" >
      <tr bgcolor="#FF990 0">
      <td width="5%" class="bold-highlite" height="20"><p> &nbsp;</p></td>
      <td width="33%" class="bold-highlite" height="20">Sur name</td>
      <td width="32%" class="bold-highlite">First Name</td>
      <td width="10%" class="small">
      <p class="bold-highlite">Activ e?</p>
      </td>
      <td width="10%" class="small">
      <p class="bold-highlite">Conta ct?</p>
      </td>
      </tr>
      <cares:OrgOffic er controlName="TA BLE" />
      <!--
      TABLE DISPLAY
      -->
      <tr bgcolor="#E7E3E 7">
      <td colspan="5">
      <p align="right">
      <cares:OrgOffic er buttonName="nav "/>
      </p>
      </td>
      </tr>
      </table>
      <p>&nbsp;</p>
      </form>
      <p class="higlite" >&nbsp;</p>
      </td>
      </tr>
      </table>
      </td>
      <td>&nbsp;</td>
      </tr>
      <tr>
      <td bgcolor="#00009 9" valign="top">
      <%@ include file="/global/SideBarLinksAdm in.jsp" %>
      <br>
      <table width="100%" border="0" cellspacing="0" cellpadding="0" >
      <tr>
      <td><img src="../images/interface/brochure_photo. jpg" width="150" height="94" vspace="4" alt="EPA photo - inspecting chemical bottle"></td>
      </tr>
      <tr>
      <td><img src="../images/interface/brochure_text.g if" width="150" height="95" alt="Protecting , restoring and enhancing the environment"></td>
      </tr>
      <tr>
      <td>&nbsp;</td>
      </tr>
      </table>
      </td>
      <td>&nbsp;</td>
      </tr>
      <tr>
      <td bgcolor="#00009 9" valign="top" align="center">
      <a href="http://www.environment .sa.gov.au/epa/index.html" target="_blank" >
      <img src="../images/splash/epa_logo_b.gif" width="66" height="75" alt="Department of Environment and Heritage Logo and Link" border="0">
      </a>
      <br>
      <br>
      </td>
      <td valign="top">&n bsp;</td>
      <td>&nbsp;</td>
      </tr>
      </table>
      </body>
      <cares:OrgOffic er controlName="ja vascript" />
      </generic:Data>
      </html>[/CODE]



      when i click on new button it opens AdminOrgOff.jsp


      [CODE=java]<%@ page contentType="te xt/html; charset=iso-8859-1" %>
      <%@ taglib uri="TagLibGene ric.tld" prefix="generic " %>
      <%@ taglib uri="TagLibAdmi nOrgOfficer.tld " prefix="cares" %>
      <html>
      <head>
      <%
      response.setHea der("Cache-Control", "no-cache");
      response.setHea der("Pragma", "no-cache");
      %>
      <title>EPA - Complaints and Reports of Environmental Significance (CARES)</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <!--include the global styles-->
      <%@ include file="/global/GlobalStyles.js p" %>
      <SCRIPT LANGUAGE = "JavaScript ">
      //include the side bar events
      <%@ include file="/global/SideBarEvents.j sp" %>

      /*function onPageSubmit(){
      var eMailRequired=d ocument.getElem entById("ORG_EM AIL_REQD").valu e;
      var officeEmail=doc ument.getElemen tById("OFF_Emai l").value;
      //var org_name=docume nt.getElementBy Id("ORG_NAME"). value;
      if(eMailRequire d == 'Y' && officeEmail == ""){
      alert("Officer Email is Required");
      return false;
      }
      if(eMailRequire d=='Y' && officeEmail!= ""){
      document.form1. action = '../servlet/controlservlet' ;
      window.location = document.form1. REDIRECT.value;
      document.form1. submit();
      }
      }
      function onPageCancel(){
      var ans = confirm('Are you sure that you want to cancel?')
      if (ans==true){
      window.location = document.form1. REDIRECT.value;
      }
      } */
      </SCRIPT>
      <link rel="stylesheet " href="../stylesheets/epa-cares-style1.css">

      </head>

      <body bgcolor="#FFFFF F" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0 " text="#333333" link="#FF9933" vlink="#339966" alink="#339966"/>
      <!-- DATA OBJECT -->

      <generic:Data objectName="<%= request.getServ letPath()%>" remoteUser="<%= request.getRemo teUser()%>">
      <!--<cares:OrgOffic er controlName="ja vascript" />-->
      <table width="100%" border="0" cellspacing="0" cellpadding="0" >
      <%@ include file="/global/TopBarLinks.jsp " %>
      <tr>
      <td bgcolor="#00009 9" valign="top"><a href="../main/main_menu.jsp"> <img src="../images/interface/epa_logo_pt2.gi f" width="150" height="38" alt="Back to Main Menu" border="0"></a></td>
      <td rowspan="2" valign="top">
      <table width="100%" border="0" cellspacing="0" cellpadding="0" >
      <tr>
      <td width="5%">&nbs p;</td>
      <td width="95%">&nb sp;</td>
      </tr>
      <tr>
      <td width="5%"><img src="../images/interface/spacer.gif" width="15" height="15"></td>
      <td width="95%" valign="top">
      <p class="main-header">Adminis tration - Organisation Officers</p>
      <form name="form1" method="post" action="">
      <cares:OrgOffic er actionFlag="tru e" />
      <cares:OrgOffic er pageName="true" />
      <input type="hidden" <cares:OrgOffic er controlName="OF F_ID" />>
      <input type="hidden" <cares:OrgOffic er controlName="RE DIRECT" />>
      <input type="hidden" <cares:OrgOffic er controlName="OF F_ORG_ID" />>
      <input type="hidden" name="OFF_LASTU PDATEUSER" value="<cares:O rgOfficer controlName="SE CURITY_OFF_USER " />">
      <table width="90%" border="0" cellspacing="2" cellpadding="3" >
      <tr bgcolor="#E7E3E 7">
      <td width="40%">
      <p align="right">O rganisation name: &nbsp;</p>
      </td>
      <td width="60%">

      <p class="blue-highlite"><care s:OrgOfficer controlName="OR G_NAME" />
      </p>
      </td>
      </tr>
      <tr bgcolor="#E7E3E 7">
      <td>
      <p align="right">O fficer Surname: &nbsp;</p>
      </td>
      <td width="1%" bgcolor="#E7E3E 7">
      <p>
      <input type="text" size="35" <cares:OrgOffic er controlName="OF F_SURNAME" />>
      </p>
      </td>
      </tr>
      <tr bgcolor="#E7E3E 7">
      <td>
      <p align="right">O fficer First Name:&nbsp;&nbs p;</p>
      </td>
      <td width="1%">
      <input type="text" size="35" <cares:OrgOffic er controlName="OF F_GIVENNAME" />>
      </td>
      </tr>
      <tr bgcolor="#E7E3E 7">
      <td valign="middle" >
      <p align="right">O fficer E-mail:&nbsp;</p>
      </td>
      <td height="38" valign="middle" width="1%">
      <input type="text" size="35" <cares:OrgOffic er controlName="OF F_EMAIL" />>
      </td>
      </tr>
      <tr bgcolor="#E7E3E 7">
      <td valign="middle" >
      <p align="right">O fficer User Name:&nbsp;</p>
      </td>
      <td height="38" valign="middle" width="1%">
      <input type="text" size="35" <cares:OrgOffic er controlName="OF F_USERNAME" />>
      </td>
      </tr>
      <tr bgcolor="#E7E3E 7">
      <td valign="middle" >
      <p align="right">K ey Contact &nbsp;</p>
      </td>
      <td height="38" valign="middle" width="1%">
      <cares:OrgOffic er controlName="OF F_KEYCONTACT" comboLoad="YESN O"/>
      </td>
      </tr>
      <tr bgcolor="#E7E3E 7">
      <td valign="middle" >
      <p align="right">A ctive &nbsp;</p>
      </td>
      <td height="38" valign="middle" width="1%">
      <cares:OrgOffic er controlName="OF F_ACTIVE" comboLoad="YESN O"/>
      </td>
      </tr>

      <!--<tr bgcolor="#E7E3E 7">
      <td valign="middle" >
      <p align="right">e-Mail Required:&nbsp; </p>
      </td>
      <td height="38" valign="middle" width="1%">
      <input type="text" size="35" <cares:OrgOffic er controlName="OR G_EMAIL_REQD" />>
      </td>
      </tr>-->
      <tr bgcolor="#FF990 0" valign="middle" >
      <td colspan="2">
      <div align="right">
      <cares:OrgOffic er buttonName="sav e_buttons" />
      </div>
      </td>
      </tr>



      <!-- <tr bgcolor="#FF990 0" valign="middle" >
      <td colspan="2">
      <div align="right">
      <input type="submit" name="button_sa ve" value="Save" onclick="javasc ript:onPageSubm it()">
      <input type="button" name="button_ca ncel" value="Cancel" onclick="javasc ript:onPageCanc el()">
      </div>
      </td>
      </tr>-->

      </table>
      <br>
      <p>&nbsp;</p>
      </form>
      <p class="higlite" >&nbsp;</p>
      </td>
      </tr>
      </table>
      </td>
      <td>&nbsp;</td>
      </tr>
      <tr>
      <td bgcolor="#00009 9" valign="top">
      <%@ include file="/global/SideBarLinksAdm in.jsp" %>
      <br>
      <table width="100%" border="0" cellspacing="0" cellpadding="0" >
      <tr>
      <td><img src="../images/interface/brochure_photo. jpg" width="150" height="94" vspace="4" alt="EPA photo - inspecting chemical bottle"></td>
      </tr>
      <tr>
      <td><img src="../images/interface/brochure_text.g if" width="150" height="95" alt="Protecting , restoring and enhancing the environment"></td>
      </tr>
      <tr>
      <td>&nbsp;</td>
      </tr>
      </table>
      </td>
      <td>&nbsp;</td>
      </tr>
      <tr>
      <td bgcolor="#00009 9" valign="top" align="center">
      <a href="http://www.environment .sa.gov.au/epa/index.html" target="_blank" >
      <img src="../images/splash/epa_logo_b.gif" width="66" height="75" alt="Department of Environment and Heritage Logo and Link" border="0">
      </a>
      <br>
      <br>
      </td>
      <td valign="top">&n bsp;</td>
      <td>&nbsp;</td>
      </tr>
      </table>
      </body>
      <cares:OrgOffic er controlName="ja vascript" />
      </generic:Data>
      </html>[/CODE]
      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=li nk communication. this is one of my organisation list.

      Please reply soon.


      Thanx in Advance
      Gaurav
      Last edited by gits; Jun 24 '08, 06:52 AM. Reason: added code tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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.
        Originally posted by Gaurav Jhamb
        i want to make it nonmandatory only when i pass organisation=li nk 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:
        [code=javascript]// assuming org is reference to passed organisation if exists
        if (!org) {
        // test for email...
        }[/code]To give something more specific, you'll have to give more details.

        Comment

        Working...