forms gets submitted


below is the code..if i click on new i validate a date and call a fucntion which returns false if invalid. the thing is that if get an alert ("Invalid date") the form gets submitted/refreshed and values input by user gets lost ..can some one help

<html>
<head>
<title> Request Line Window  </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript1.2">
var field1='<%=request.getParameter("field1")%>';
var field2='<%=request.getParameter("field2")%>';
var field3='<%=request.getParameter("field3")%>';
var field4='<%=request.getParameter("field4")%>';
var field5='<%=request.getParameter("field5")%>';
var field6='<%=request.getParameter("field6")%>';
field1='requestAction';
field2='requestCategory';
field3='requestEntity';
field4='requestClass';
field5='requestDollar';
field6='requestTerminationDate';

function validateDollarChange()
{
      if (document.lineItemForm.selSigDollar.value == "Other")
      {
            document.lineItemForm.dollarlimittxt.style.display = "inline";
      }else
      {
            document.lineItemForm.dollarlimittxt.style.display = "none";
      }
}

function test(requestActionVar)
{
var categoryOptionScriptVar = "";
var sigClassOptionScriptVar = "";
var terminationDateVar = "";
var signatoryTypeScriptVar = "";
var signatoryIdScriptVar = "";
var entityOptionScriptVar = "";
var dollarOptionScriptVar ="";
var effectiveDate = document.lineItemForm.terminationDate.value;
//alert(effectiveDate);

if (!validDates(effectiveDate))
{
  alert("Please select a Valid Date");
  return true;
}

else
      {
            //alert(document.lineItemForm.selEntity.length);
            //opener.addRequestItem(requestActionVar,"categoryOptionScriptVar,entityOptionScriptVar,sigClassOptionScriptVar,terminationDateVar);
            for (var i= 0;i < document.lineItemForm.selEntity.length;i++)
            {
                  if(document.lineItemForm.selEntity.options[i].selected)
                    {
                                  // alert("in for loop");
                                  categoryOptionScriptVar = document.lineItemForm.selCategory.value;
                                  //alert(categoryOptionScriptVar);
                             entityOptionScriptVar = entityOptionScriptVar + document.lineItemForm.selEntity.options[i].value + ":";
                             //alert(entityOptionScriptVar);
                                  sigClassOptionScriptVar = document.lineItemForm.selSigClass.value;
                                 //alert(sigClassOptionScriptVar);
                                 //dollarOptionScriptVar=document.lineItemForm.selSigDollar.options[document.lineItemForm.selSigDollar.selectedIndex].value;
                              // alert(dollarOptionScriptVar);
                                 terminationDateVar = document.lineItemForm.terminationDate.value;
                                 //alert(terminationDateVar);
                                 if(document.lineItemForm.selSigDollar.options[document.lineItemForm.selSigDollar.selectedIndex].value == "Other")
                                             {
                                                 dollarOptionScriptVar=document.lineItemForm.dollarlimittxt.value;
                                             }
                                 else
                                       {
                                                   dollarOptionScriptVar=document.lineItemForm.selSigDollar.options[document.lineItemForm.selSigDollar.selectedIndex].value;
                                       }
                                // alert("dollarOptionScriptVar is ...." +dollarOptionScriptVar);
               
                       }      
                    }

      opener.addRequestIntItem(requestActionVar,categoryOptionScriptVar,entityOptionScriptVar,sigClassOptionScriptVar,dollarOptionScriptVar,terminationDateVar);
      window.close();
}
}
function validDates(effectiveDate)

{
var effective_year = effectiveDate.substring(7,11);
var effective_mon  = effectiveDate.substring(0,3);
var effective_day  = effectiveDate.substring(4,6);
//alert(effectiveDate);
var sDate = new Date(effectiveDate);

var currentDate = new Date();
//alert(currentDate);
var currentDates = "" +currentDate;
var current_year = currentDates.substring(24,29);
var current_mon  = currentDates.substring(4,7);
var current_day  = currentDates.substring(8,10);
var currentDates1 = current_mon + " " +current_day + ", " + current_year;
//alert(currentDates1);
var cDate = new Date(currentDates1);

var latest_year = currentDate.getYear() +2;
var latest_mon  = current_mon;
var latest_day  = current_day;
var latestDate = latest_mon + " " + latest_day + ", " + latest_year;
alert(latestDate);
var eDate= new Date(latestDate)

var terminationDateValue ="";

//check if Date is blank assign 2 years from Current Date
//alert(sDate);
//alert(cDate);
//alert(eDate);

if( effectiveDate == "")

{
   alert("when eff date blank" );
   document.lineItemForm.terminationDate.value=latestDate ;
   return true;
}

else if(sDate < cDate)
{
   alert("Please select a valid Date greater than Today");
   document.lineItemForm.terminationDate.focus();
   return false;
}

else if(sDate > eDate)
{
    alert(effectiveDate);
    alert(latestDate);
    alert(" Please select a valid Date less than 2 years");
    document.lineItemForm.terminationDate.focus();
    return false;
}

else
{
    //alert("Valid Date");
   // document.lineItemForm.terminationDate.value=effectiveDate;
    return true;
}

}
</script>

<SCRIPT FOR=window EVENT=onload LANGUAGE="JScript">
      document.lineItemForm.dollarlimittxt.style.display = "none";
</SCRIPT>

<%


String signatoryTypeVar = "";
String signatoryIdVar = "";
if (request.getParameter("signatoryType")!=null )
{
        signatoryTypeVar = request.getParameter("signatoryType");
}
if (request.getParameter("signatoryId")!=null )
{
        signatoryIdVar = request.getParameter("signatoryId");
}

//int selectedEntityInt = 0;
//String selectedEntity = "";
//if (request.getParameter("hiddenEntity")!=null)
//{
//       selectedEntity = request.getParameter("hiddenEntity");
//        try {
//                selectedEntityInt = Integer.parseInt(selectedEntity);
//            }
//        catch  (NumberFormatException badFormatSelectedIndex)
//       {
//                selectedEntityInt = 0;
//        }
//}


//int selectedCategoryInt = 0;
//String selectedCategory = "External";
//if (request.getParameter("hiddenCategory") != null)
//{
//        selectedCategory = request.getParameter("hiddenCategory");
//        try {
//                selectedCategoryInt = Integer.parseInt(selectedCategory);
//            }
//        catch  (NumberFormatException badFormatSelectedIndex)
//        {
//                selectedCategoryInt = 0;
//        }
//}

//String keyValuesSubmitted = "";
//if (request.getParameter("keyValuesSubmittedBool") != null)
//{
//        keyValuesSubmitted = request.getParameter("keyValuesSubmittedBool");
//}
// Parameters for the actual values -not their indexes - selected in the list boxes
//String selectedEntityText = "ALL";
//if (request.getParameter("selectedEntityText") !=null)
//{
//        selectedEntityText = request.getParameter("selectedEntityText");
//}

//String selectedCategoryText = "ALL";
//if (request.getParameter("selectedCategoryText") !=null)
//{
//        selectedCategoryText = request.getParameter("selectedCategoryText");
//}

%>

</head>
<jsp:usebean id="sigstatic"
      class="com.lehman.tres.sig.servlets.SIG_StaticDataList"
      scope="request" />
<%
      // GET VALID VALUES OF THE VARIOUS STATIC DATA ELEMENTS
      // FIRST LIST OF ENTITIES
      String  genericOptionXsl ="../wls61/config/etg/applications/TWS/SIG/xsl/genericOption.xsl";
      String  sigClassOptionXsl="../wls61/config/etg/applications/TWS/SIG/xsl/sigClassList.xsl";
      String strEntityXML = "";
      try {
            strEntityXML = sigstatic.generateXMLStaticDataList ("entity"); // get list of entities
      }
      catch(Exception e)
      {
            out.println ( "Error:  error calling generateXMLStaticDataList with param entity ");
      }
      // NEXT LIST OF CATEGORIES
      String strCategoryXML ="";
      try {
            strCategoryXML = sigstatic.generateXMLStaticDataList ("category"); // get list of categories (internal & external currently)
      }
      catch(Exception e)
      {
             out.println ( "Error:  error calling generateXMLStaticDataList with param category");
      }
      %>

<body marginheight="0" marginwidth="0" topmargin="0" leftmargin="0">

<form name="lineItemForm">

<input type="hidden" name="keyValuesSubmittedBool" value="0" >
<input type="hidden" name="signatoryId" value=<%=signatoryIdVar %> >
<input type="hidden" name="signatoryType" value=<%=signatoryTypeVar %> >
<input type="hidden" name="terminationDate2" value="?" > 

<table width="740" cellpadding="0" cellspacing="0" border="0">
      <tr>
            <td width="10" rowspan="99"><img src="/LL_S/imagelibrary/spacer.gif"
height="1" width="10" border="0"></td>
            <td width="309" height="37" valign="top" align="left"><img src="/LL_S/imagelibrary/logos/ll_popup.gif" width="309" height="37"  border="0"></td>
            <!-- <td width="309" height="37" valign="top" align="left"><<img src="/LL_S/imagelibrary/logos/ll_popup.gif" width="309" height="37"  border="0"></td>-->
            <td width="10" rowspan="99"><img src="/LL_S/imagelibrary/spacer.gif" height="1" width="10" border="0"></td>
      </tr>
      <tr>
            <td class="bggray" height="1" valign="top"><img src="/LL_S/imagelibrary/spacer.gif" height="1" width="1" border="0"></td>
      </tr>
      <tr>
            <td height="10"><img src="/LL_S/imagelibrary/spacer.gif" height="10" width="1" border="0"></td>
      </tr>
        <tr>
            <td bgcolor="#FFFFFF" width="100%" align="center">
            <!-- Nugget Code for Pop Up Goes here!!! -->
            <table width="100%" cellpadding="1" cellspacing="0" border="0" class="bggray">
            <tr><td>
               <table width="100%" cellpadding="0" cellspacing="0" border="0" class="bgcoolgray">
                  <tr>
                     <td class="bggray" colspan="3" height="22" valign="middle"><span class="a14white">&nbsp;Signatory Request Details</span></td>
                  </tr>
                  <tr>
                     <td width="5" rowspan="2"><img src="/LL_S/imagelibrary/artwork/spacer.gif" height="1" width="5" border="0"></td>
                     <td height="5"><img src="/LL_S/imagelibrary/artwork/spacer.gif" height="1" width="5" border="0"></td>
                     <td width="5" rowspan="2"><img src="/LL_S/imagelibrary/artwork/spacer.gif" height="1" width="5" border="0"></td>
                  </tr>
                  <tr>
                  <td>
                     <table width="100%" cellpadding="2" cellspacing=0 border=0>
                     <tr>
                        <td><span class="headingTwo">&nbsp;Category&nbsp;&nbsp;&nbsp;</span></td>
                        <td><span class="headingTwo">&nbsp;Entity</span></td>
                        <td><span class="headingTwo">&nbsp;Class&nbsp;&nbsp;&nbsp;&nbsp;</span></td>
                        <td><span class="headingTwo">&nbsp;Dollar Limit&nbsp;&nbsp;&nbsp;&nbsp;</span></td>
                        <td><span class="headingTwo">&nbsp;Termination Date</span></td>
                     </tr>
                     <tr>
                            <td colspan="1">
                                      <input name="selCategory" class="formtext" value="Internal" readonly>
                                 <%
                   //                 out.println(XSLTransformer.transform(strCategoryXML, genericOptionXsl));
                                 %>
                            
                            </td>
                            <td colspan="1">
                                      <select name="selEntity" multiple class="formtext"  >
                                    <%
                                    out.println(XSLTransformer.transform(strEntityXML, genericOptionXsl));
                                    %>
               
                        </td>
                                           <td colspan="1">
                                    <input name="selSigClass" class="formtext" value="I" readonly>
                              
                       </td>            

                                           <td colspan="1">
                                    <SELECT name="selSigDollar" class="formtext" onchange="validateDollarChange();">
                                    <OPTION value="<$999,999.99">Up to $999,999.99</OPTION>
                                    <OPTION value="<$9,999,999.99">Up to $9,999,999.99</OPTION>
                                    <OPTION value="<$49,999,999.99">Up to $49,999,999.99</OPTION>
                                    <OPTION value=">$50,000,000 ">$50,000,000 and over</OPTION>
                                    <OPTION value="Other">Other</OPTION>
                                </SELECT>
                                
                                                
                                                </td>      

                       <td nowrap valign="middle" >
                                <INPUT name="terminationDate" size="25" type="text" value="" class="formtext" readonly>
                           <img border="0" style="cursor: hand;" onClick="openCalendar('terminationDate','terminationDate2',document.forms[0].name,'Daily','false');" src="/LL_S/imagelibrary/icons/calendar.gif" >
                        </td>
                           </tr>
                           
                      <!-- added for Textbox-->
                  <tr>
                        <td colspan="4" align="right"><input name="dollarlimittxt" display="none" type="text" value="Enter a Dollar Limit Value"/></td>
                  </tr>    
                           
                           </table>
                           
                           <!-- NOW ADD BUTTONS - NEW IF NO EXISTING POWER FOUND; UPDATE OR REMOVE IF EXISTING POWER FOUND -->
                  </td></tr>
                  
                              
                  <tr>
                  <!-- add appropriate buttons -->
        <td colspan="3" height="5"><img src="/LL_S/imagelibrary/artwork/spacer.gif" height="5" width="1" border="0"></td>
        </tr>
        <tr>
        <td colspan="3" class="bgceladon" height="22" align="right" valign="middle"> <img src="/LL_S/imagelibrary/artwork/spacer.gif
" alt="Transparent Spacer " border="0" height="18" width="5">
<input type="image" src="/LL_S/imagelibrary/buttons/new.gif"    width="39" height="18"
                  onClick="javascript:test('NEW');">
                  <img src="/LL_S/imagelibrary/artwork/spacer.gif" height="22" width="5">  
                      
        <tr>
 
      </table>
</body>
</html>

bharat_tcsAsked:
Who is Participating?
 
FahdmurtazaCommented:
Two things..just store the form inputs on submit in session variables ans just let them be filled in form fields. This way you can save them for latter use and the form values would be retained.

Also if you don't want to submitt on wrong date just change the code i.e

if (!validDates(effectiveDate))
{
  alert("Please select a Valid Date");
  return true;
}

to

if (!validDates(effectiveDate))
{
  alert("Please select a Valid Date");
  return false;
}

Hope you got it!

Regards,
Fahd Murtaza
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.