Link to home
Start Free TrialLog in
Avatar of sureshp
sureshp

asked on

Pls check this code...

hi,
if you enter 1 in the period text box(initially it shows wrong output).
If i input period=10 shows undefined month.
In netscape it shows wrong year display(that's why i add 1900).
Pls go thru this code and provide a solution.
code:
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script language="JavaScript"><!--
function change(){
var month = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
period = document.renew.period.value;
newDate = new Date();
var sur = document.renew.newexpdate.value;
var s= sur.split('-');
newDate.setMonth(newDate.getMonth()+parseInt(period));
if(s[0] >28)
{
  if (newDate.getYear() < 1900)
 {
  newExp =1+"-"+month[newDate.getMonth()]+"-"+[newDate.getYear()+1900];
 }
 else
 {
  newExp = 1+"-"+month[newDate.getMonth()]+"-"+newDate.getYear();
 }
}
if(s[0] <=28)
 {
  if (newDate.getYear() < 1900)
 {
  newExp =s[0]+"-"+month[newDate.getMonth()-1]+"-"+[newDate.getYear()+1900];
 }
 else
 {
  newExp = s[0]+"-"+month[newDate.getMonth()-1]+"-"+newDate.getYear();
  }
 }
document.renew.newexpdate.value = newExp;

}

// -->
</script>
</HEAD>
<BODY background="/images/com/uu_nhome_bg.gif">
<form name="renew" action="/cgi/com/xyz.cgi" method="post">
<table width="85%" border="0" cellspacing="0" cellpadding="0">
   
 <tr>
      <td>
        <div align="right"><font face="Arial, Helvetica, sans-serif" size="2">Extra
          period : </font></div>
      </td>
      <td>
        <input type="text" name="period" value="" onChange="change();">in months
      </td>
 </tr>

 
    <tr>
      <td>
        <div align="right"><font face="Arial, Helvetica, sans-serif" size="2">Current Expiry
          Date : </font></div>
      </td>
      <td>
        <input type="text" name="newexpdate" value="27-Jan-2001" readonly>
      </td>
    </tr>
 
     <tr>
      <td>
        <div align="right">
          <input type="submit" name="btnAddSpace" value=" Add/Renew" >
        </div>
      </td>
      <td>
        <input type="button" name="btnCancel" value="  Cancel  " onClick="javascript:window.close()">
      </td>
    </tr>
  </table>
</form>
</BODY>
</HTML>


Thanks in advance.
Avatar of a.marsh
a.marsh

Firstly, there are some pieces of the code that won't work in Netscape e.g. readonly

I shall have a look and try and improve the script for you. :o)

Ant
Avatar of sureshp

ASKER

a.marsh,
Thanks for your comment.
modified some changes in my previous code.
 This code
 if(s[0] <=28)
{
 if (newDate.getYear() < 1900)
{
 newExp =s[0]+"-"+month[newDate.getMonth()-1]+"-"+[newDate.getYear()+1900];
}
else
{
 newExp = s[0]+"-"+month[newDate.getMonth()-1]+"-"+newDate.getYear();
 }
}
working properly on client machine. If i upload to the server ., instead of this s[0]+"-"+month[newDate.getMonth()-1] statement i should use s[0]+"-"+month[newDate.getMonth()+1] .
Why it happens?
Thanks in advance.
Is the date on your server correct...?? I know that sounds silly...but....it would be silly if your server handles the getMonth() differently....

Cheers,
Max.
+1 is the correct behavior. Month counting starts at 0 (january)...there shouldn't ever be a -1 or something like that...

regards,
CJ
just a note on the getYear thing.
Use getFullYear() which is supported by the recent browser versions(NN4+ and IE4+). Then you don't have to keep adjusting for year<1900 and so on.
hai,
check this code:
It works fine and the 11 problem eliminated.

code
-------

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function period_onchange() {
var month = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
period = document.renew.period.value;
alert(period)
newDate = new Date();
alert(newDate)
var sur = document.renew.newexpdate.value;
alert(sur)
var s= sur.split('-');
alert(s)
newDate.setMonth(newDate.getMonth()+parseInt(period));
if(s[0] >28)
{
 if (newDate.getYear() < 1900)
{
 newExp =1+"-"+month[newDate.getMonth()]+"-"+[newDate.getYear()+1900];
alert("1")
alert(newExp)
}
else
{
 newExp = 1+"-"+month[newDate.getMonth()]+"-"+newDate.getYear();
 alert("2")
 alert(newExp)
}
}

if(s[0] <=28)
{
 if (newDate.getYear() < 1900)
 
{
 newExp =s[0]+"-"+month[newDate.getMonth()+1]+"-"+[newDate.getYear()+1900];
 alert("3")
 alert(newExp)
}
else
{
 newExp = s[0]+"-"+month[newDate.getMonth()+1]+"-"+newDate.getYear();
 alert("4")
 alert(newExp)
 }
}
alert("5")
alert(newExp)
document.renew.newexpdate.value = newExp;


}
//-->
</SCRIPT>
</HEAD>
<BODY background="/images/com/uu_nhome_bg.gif">
<form name="renew" action="/cgi/com/xyz.cgi" method="post">
<table width="85%" border="0" cellspacing="0" cellpadding="0">
 
<tr>
     <td>
       <div align="right"><font face="Arial, Helvetica, sans-serif" size="2">Extra
         period : </font></div>
     </td>
     <td>
       <input type="text" name="period" value=""  LANGUAGE=javascript onchange="return period_onchange()">in months
     </td>
</tr>


   <tr>
     <td>
       <div align="right"><font face="Arial, Helvetica, sans-serif" size="2">Current Expiry
         Date : </font></div>
     </td>
     <td>
       <input type="text" name="newexpdate" value="27-Jan-2001" readonly>
     </td>
   </tr>
 
    <tr>
     <td>
       <div align="right">
         <input type="submit" name="btnAddSpace" value=" Add/Renew" >
       </div>
     </td>
     <td>
       <input type="button" name="btnCancel" value="  Cancel  " onClick="javascript:window.close()">
     </td>
   </tr>
 </table>
</form>
</BODY>
</HTML>

with cheer.
Avatar of sureshp

ASKER

ksenthilraj,
if i input period=10 shows undefined month.
pls check for 1 to 12.
Thanks
Your question is not clear. I think you want to add the number of extra period of months to the existing expiry date to get the new expiry date. If so then you can use the function below else please explain your requirement clearly.

function change()
{
     var month = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
     period = document.renew.period.value;
     var newExp = "";
     var sur = document.renew.newexpdate.value;
     var s = sur.split("-");
        var mon;
     for(i=0;i<month.length;i++)
     {
          if (month[i]==s[1])
               mon = i;
     }
     var newDate = new Date(s[2],mon,s[0]);
     newDate.setMonth(newDate.getMonth()+parseInt(period));
     if(s[0]>28)
     {
          newExp = 1 + "-" + month[newDate.getMonth()] + "-" + newDate.getFullYear();
     }
     else if(s[0]<=28)
     {
          newExp = s[0] + "-" + month[newDate.getMonth()] + "-" + newDate.getFullYear();
     }
     document.renew.newexpdate.value = newExp;
}
</script>
Avatar of sureshp

ASKER

nathan_d_S,
Thanks for your comment.
I will check your code and reply.
Avatar of sureshp

ASKER

nathan_d_S,
Thanks a lot.
Using this script., if i enter a period =1 it shows correct increment in month., again i enter in the same column period =2 it updates date from previous result value. How can i avoid this.My Earlier code doesn't have this problem.
Check this code:
 
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function change()
{
    var month = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    period = document.renew.period.value;
    var newExp = "";
    var sur = document.renew.newexpdate.value;
    var s = sur.split("-");
       var mon;
    for(i=0;i<month.length;i++)
    {
         if (month[i]==s[1])
              mon = i;
    }
    var newDate = new Date(s[2],mon,s[0]);
    newDate.setMonth(newDate.getMonth()+parseInt(period));
    if(s[0]>28)
    {
         newExp = 1 + "-" + month[newDate.getMonth()] + "-" + newDate.getFullYear();
    }
    else if(s[0]<=28)
    {
         newExp = s[0] + "-" + month[newDate.getMonth()] + "-" + newDate.getFullYear();
    }
    document.renew.newexpdate.value = newExp;
}
//-->
</script>

</HEAD>
<BODY background="/images/com/uu_nhome_bg.gif">
<form >
 
<tr>
    <td>
      <div align="right"><font face="Arial, Helvetica, sans-serif" size="2">Extra
        period : </font></div>
    </td>
    <td>
      <input type="text" name="period" value=""  LANGUAGE=javascript onchange="return period_onchange()">in
months
    </td>
</tr>


  <tr>
    <td>
      <div align="right"><font face="Arial, Helvetica, sans-serif" size="2">Current Expiry
        Date : </font></div>
    </td>
    <td>
      <input type="text" name="newexpdate" value="27-Jan-2001" readonly>
    </td>
  </tr>

   <tr>
    <td>
      <div align="right">
        <input type="submit" name="btnAddSpace" value=" Add/Renew" >
      </div>
    </td>
    <td>
      <input type="button" name="btnCancel" value="  Cancel  " onClick="javascript:window.close()">
    </td>
  </tr>
</table>
</form>
</BODY>
</HTML>

Hope you can do this.
Thanks in advance

You have pended this question for deletion, however, the deletion did not complete.

Comment here please as to why you want to delete this rather than award points.  Until the autodelete feature and other programming changes that are in process are completed, it'll require our intervention to help you with this.  It is always recommended that you let the experts know by posting comments first, of your intention to delete a question where there has been activity.

Listening further.

Moondancer
Community Support Moderator @ Experts Exchange
ASKER CERTIFIED SOLUTION
Avatar of nathan_d_S
nathan_d_S

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please return and update/finalize all your open questions.  If you need our help, let us know.  If the questions remain unresolved next week, we will need to take the actions to close them.

Your cooperation in managing your questions at this site is not only appreciated but necessary.

Topic Area              URL              Date              
Community Support (General)   https://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt&qid=20252119  01/06/02  
JavaScript   https://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20069836  02/02/01  
JavaScript   https://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20076966  02/13/01  
JavaScript   https://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20227582  11/06/01  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=11326038  09/20/00  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20182722  09/13/01  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20182782  09/14/01  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20247168  12/18/01  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20247820  12/19/01  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20253473  01/10/02  

Moondancer
Community Support Moderator @ Experts Exchange
Unresponsive to follow up requests, finalized today.  Admin has been advised.
Moondancer
Community Support Moderator @ Experts Exchange