Major problem with my javascript

Discussion in 'JavaScript' started by Luke Watson, Oct 26, 2010.

  1. #1
    Hey guys i've finished my code for this program which you can view here,

    
    <!-- My Program --> 
    <html> 
      <head> 
        <title>My Program</title> 
        <style type="text/css"> 
          body {
                 background-color: #000000;
                 color: #FFFFFF;
                 font-size: 30;
                 text-align: center;
               }
        </style> 
        <script type="text/javascript"> 
        function calculate()
      {
      var amount, repayment, interest, total_interest, month_interest, term_loan, rate;
     
      if (document.frmcalculateloan.txtloan.value == "")
             {
                alert("Please enter your loan amount");
                document.frmcalculateloan.txtloan.focus();
             }
             else if (document.frmcalculateloan.txtloan.value != parseFloat(document.frmcalculateloan.txtloan.value))
             {
                alert("Your loan amount must be a number");
                document.frmcalculateloan.txtloan.focus();
             }
             else if (document.frmcalculateloan.txtmonthly.value == "")
             {
                alert("Please enter your monthly loan repayment");
                document.frmcalculateloan.txtmonthly.focus();
             }
             else if (document.frmcalculateloan.txtmonthly.value != parseFloat(document.frmcalculateloan.txtmonthly.value))
             {
                alert("Your monthly loan repayment must be a number");
                document.frmcalculateloan.txtmonthly.focus();
             }
             else if (document.frmcalculateloan.txtint_rate.value == "")
             {
                alert("Please enter your interest rate");
                document.frmcalculateloan.txtint_rate.focus();
             }
             else if (document.frmcalculateloan.txtint_rate.value != parseFloat(document.frmcalculateloan.txtint_rate.value))
             {
                alert("Your interest rate must be a number");
                document.frmcalculateloan.txtint_rate.focus();
             }
     
      term_loan = 0;
      total_interest = 0;
      amount = parseFloat(document.frmcalculateloan.txtloan.value);
      repayment = parseFloat(document.frmcalculateloan.txtmonthly.value);
      rate = parseFloat(document.frmcalculateloan.txtint_rate.value);
     
      if (repayment < rate)
      {}
     
      else
      {
      while (amount > 0)
      {
     
      if (repayment < amount)
      {  
    	amount = amount - repayment; 	  
    	month_interest = (amount * (rate / 100) / 12);			 
    	total_interest = total_interest + month_interest; 
    	amount = amount + month_interest;			 
      }
      else 
      {
    	amount = 0;
      }
     
    	term_loan = term_loan + 1;
      } 
    				
    	document.frmcalculateloan.readonly_term.value = parseInt(term_loan/12) + " years and " + (term_loan % 12) + "  months";
    	document.frmcalculateloan.readonly_total_interest.value = "$" + total_interest.toFixed(2);
      }
      }	
     
        function clean()
           {
             //alert("in clean");
             document.frmcalculateloan.txtloan.value = "";
    		 document.frmcalculateloan.txtmonthly.value = "";
                     document.frmcalculateloan.txtint_rate.value = "";
             document.frmcalculateloan.txtloan.value = "";
             document.frmcalculateloan.readonly_term.value = "";
             document.frmcalculateloan.readonly_total_interest.value = "";
             document.frmcalculateloan.txtloan.focus();
    		 document.frmcalculateloan.txtmonthly.focus();
             document.frmcalculateloan.txtloan.focus();
                     document.frmcalculateloan.txtLoanAmount.focus();
           }
     
        </script> 
        </head> 
        <body onload="window.document.frmcalculateloan.txtloan.focus()"> 
     
      <h1>My Program</h1> 
      <form name="frmcalculateloan"> 
      Enter the Loan Amount : $<input type="text" name="txtloan"  class="txtbox" /><br /><br /> 
      Enter the Monthly Loan Repayment : $<input type="text" name="txtmonthly"  class="txtbox" /><br /><br /> 
      Enter the Interest Rate : <input type="text" name="txtint_rate"  class="txtbox" /> &nbsp; %
      <br /><br /><br /> 
     
      <hr> 
      Term Of Loan: <input type="text" name="readonly_term" id="readonly_term"  disabled="READONLY" class="READONLY" /> 
      <br /><br /> 
      Total Interest Paid: <input type="text" name="readonly_total_interest" id="readonly_total_interest"  disabled="READONLY" class="disabled" /> 
      </form> 
      <br /> <br /> 
      <input type="button" name="cmdcalculate" value="Calculate term of loan and total interest" onclick="calculate()" /> 
      &nbsp;&nbsp;&nbsp;
     
      <input type="button" name="cmdClear" value="Reset" onClick="clean()"> 
        
       </body> 
      </html> 
    
    Code (markup):
    It all works fine except when the user clicks the "calculate term of loan and total interest" button i don't want to see any text in the disabled textbox. How would i do this? Many thanks to whoever can help.
     
    Last edited: Oct 26, 2010
    Luke Watson, Oct 26, 2010 IP
  2. Bigcats82

    Bigcats82 Greenhorn

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    Which textbox do you want to be blank?

    document.txtName.value = "";
     
    Bigcats82, Oct 26, 2010 IP
  3. Luke Watson

    Luke Watson Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I would like these two disabled textboxes to be blank when the user clicks the calculate button,

    
    Term Of Loan: <input type="text" name="readonly_term" id="readonly_term"  disabled="READONLY" class="READONLY" /> 
      <br /><br /> 
      Total Interest Paid: <input type="text" name="readonly_total_interest" id="readonly_total_interest"  disabled="READONLY" class="disabled" />
    
    Code (markup):
    It should only display text if the user has actually keyed in the loan amount, monthly loan repayment and interest rate.
     
    Luke Watson, Oct 26, 2010 IP
  4. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #4
    You could add "return false;" after your alert / focus things.
     
    wing, Oct 26, 2010 IP
  5. Luke Watson

    Luke Watson Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How do i do that?

    I'm a beginner at javascript so i'm not sure.
     
    Luke Watson, Oct 26, 2010 IP
  6. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #6
      if (document.frmcalculateloan.txtloan.value == "")
             {
                alert("Please enter your loan amount");
                document.frmcalculateloan.txtloan.focus();
                return false;
             }
             else if (document.frmcalculateloan.txtloan.value != parseFloat(document.frmcalculateloan.txtloan.value))
             {
                alert("Your loan amount must be a number");
                document.frmcalculateloan.txtloan.focus();
                return false;
             }
    Code (markup):
    etc...
     
    wing, Oct 26, 2010 IP