Objectives - to illustrate some advanced JavaScript programming concepts that include forms, buttons, text boxes, and data verification.
The idea behind this web page is to --
show how to build a scrolling message in a text box, and show how to build a form, show how to verify data entered on that form, use the data collected to calculate and display a result. The Scrolling Message
Initial code for the applet -- <html> <head> <title>Home Finders Nationwide Realty</title> <!----------------------------------------------> <script language="javascript"> <!--Hide from old browsers
Scrolling message has 4 components -
display object- defines where the scrolling object displays the message - text string assigned to a variable the position - the starting location in whichthe message first displays in the display object the delay - the length of time between when a message ends and when it starts to appear again The logic behind the function is simple: take the message, concatenate the end/start msgSpace (to indicate the end of the message and the beginning), and put it into a text box. A recursive call to the function every 200 milliseconds changes the message in the text box by moving the leftmost character to the end of the string, and sliding all the other characters left 1 position Here's the code -- var scrollMsg = "Mortgage rates are at their LOWEST !!" var msgSpace = "--- ---" var beginPos = 0 function scrollingMsg(){//
// msgForm - the name we gave to the form containing the textBox
// textBox - the name we gave to the text box on the form
// Both the form and the text box are defined below within the HTML body
//
document.msgForm.textBox.value =scrollMsg.substring(beginPos,scrollMsg.length)+msgSpace+scrollMsg.substring(0,beginPos)beginPos = beginPos + 1
if (beginPos > scrollMsg.length) {
beginPos = 0
}//
// recursive function call to scrollingMsg() using setTimeout
//window.setTimeout("scrollingMsg()",200)
}
| Next, we define the doMort() function which clears each text box corresponding to mortgage calculation values; specifically, Amount, Rate, Years, and Payment |
function doMort() { //
// first, statements to clear the text boxes ...
// document.MortCalc.Amount.value=" " document.MortCalc.Rate.value=" " document.MortCalc.Years.value=" " document.MortCalc.Payment.value=" " document.MortCalc.Amount.focus()
}
| The Calc(myform) function receives the mortgage calculation form as a parameter. This indirectly gives the function access to the values in the text boxes for Amount, Years, Rate and Payment. | |
| The function converts & stores the text box values as integer or floating point temporary variables | |
| This function passes the value of the text box to a variable, uses the parseInt() and parseFloat() functios to convert the variable to a number, and then uses the isNaN() function to verify the value is a number. |
function Calc(myform) { //
// Here, MortCalc is the form containing the text boxes for
// Amount, Rate, Years, and Payment
//
var mortAmount = document.MortCalc.Amount.value
var mortAmount = parseInt(mortAmount,10) // 10 means base 10 if (isNaN(mortAmount)) {else {alert("The loan amount is not a number")
document.MortCalc.Amount.value=" "
document.MortCalc.Amount.focus()
}//
// check the other fields ...
//var mortRate = document.MortCalc.Rate.value
var mortRate = parseFloat(mortRate)
if (isNaN(mortRate)) {alert("The interest rate is not a number")
document.MortCalc.Rate.value=" "
document.MortCalc.Rate.focus()
}else {
var mortYears = document.MortCalc.Years.value
var mortYears = parseInt(mortYears,10)
if (isNaN(mortYears)) {alert("Number of years is not a number")
document.MortCalc.Years.value = " "
document.MortCalc.Years.focus()
}}
document.MortCalc.Payment.value =}
//
// so if everything is valid, call on the monthly() function
// to compute the monthly payment
//
monthly(mortAmount, mortRate, mortYears) } // end of function Calc
| The monthly() function takes 3 parameters (amount being borrowed, the interest rate, and the number of years that it will take to payoff the loan, and calculates the monthly payment. This uses the same technique/formula as we used for the Java example in Applets - Part 6 |
//The HTML Body ...
// the monthly() function - following the formula
// function monthly(mortAmount, mortRate, mortYears) {
var Irate = mortRate / 1200
var Pmts = mortYears * 12
var Loan = mortAmount return Loan * (Irate / (1 - (1 / Math.pow(1+Irate,Pmts))))
} //--> </script><!---------------------------------------------->
</head>
| First, the scrollingMsg() function is called when the web page is first loaded. This is done by using the onload event handler |
<body onload="scrollingMsg()">
| We define the web page itself. The very top
displays the logo of the fictitious Home Finders Nationwide Realty company | |
<p ALIGN="CENTER"><img SRC="HOMELOGO.JPG"> </p> | |
| For the top portion of the page, we define a table that consists of 3 rows, and 3 columns. In HTML, you define the cell contents in row-major order. Row 2 column 2 is where we place the text box used for the scrolling message. Note, <tr> indicates the start of a new row, <td> indicates the start of the definition of what is in a cell. And </tr> and </td> each represent ends. Note, too, that <p> is the start of a new paragraph, and <br> indicates a break. |
<div align="center"><center> <table BORDER="0" WIDTH="75%"> <tr> <td></td> <td><p ALIGN="CENTER"><img SRC="calculator.jpg" WIDTH="100" HEIGHT="106" ALT="Check out Rates"></td> <td></td> </tr> <tr> <td><p ALIGN="CENTER"><img SRC="house1.gif" WIDTH="180" HEIGHT="144"> </td> <P><!----------------------></P>
<P><!-- LoanCalc is a bookmark reference. If "Estimate Mortgage Payments" is clicked, your browser will go to the position on the page where "LoanCalc" is defined></P> <P><!----------------------></P>
<P><!-- doMort() is called if the bookmark reference is clicked to clear the text boxes for the mortgage calculation></P><td><p ALIGN="CENTER"><a HREF="#LoanCalc" onclick="doMort()">Estimate Mortgage Payment</a>
</p> <p><!----------------------------------------------> </p>
<P><Here's the definition of the msgForm and the textBox used for the scrolling message></P> <form Name="msgForm"> <p><input Type="text" Name="textBox" Size="33"> <!----------------------------------------------></p> </form> <p> </td> <td><p ALIGN="CENTER"><img SRC="house2.gif" WIDTH="180" HEIGHT="144"></td> </tr> <tr> <td></td> <td><img SRC="house3.gif" WIDTH="180" HEIGHT="144"></td> <td></td> </tr> </table>
</center></div> <p ALIGN="CENTER"><br>
| Next, we define the form MortCalc which will contain the text boxes for Amount, Rate, Years, and Payment. Note the first a NAME= reference here defines the LoanCalc bookmark referenced above. |
<a NAME="LoanCalc"></p>
<h3 ALIGN="CENTER">Estimate Mortgage Payments</h3> </a> <form Name="MortCalc"> <table> <tr> <td>Amount of Mortgage:</td> <td><input Type="text" Name="Amount" value=" " Size="9"></td> </tr> <tr> <td>Interest Rate as % (e.g. 7.9):</td> <td><input Type="text" Name="Rate" value=" " Size="9"></td> </tr> <tr> <td>Number of Years:</td> <td><input Type="text" Name="Years" value=" " Size="9"></td> </tr> <tr> <td>Monthly Payment:</td> <td><input Type="text" Name="Payment" value=" " Size="12"></td> </tr> <tr>
| Call on the Calc function with a mouse click to the calculate button and pass the MortCalc form so that the payment calculation can be made and displayed. Note the definition of the RESET button as part of the form definition. |
<td><input Type="Button" value="Calculate" onclick="Calc(MortCalc)"> <input Type="Reset"></td> </tr> </table> </form> <hr> <p><br> <br> </p> </body> </html>
| Click HERE to see the whole thing -- |
![]()