HTML and CSS Reference
In-Depth Information
To Enter the onBlur Event Handler to Call the validSalesAmt() Function
After adding the user-defined function to validate the sales price and the down payment values and calculate
the loan amount, you need to add an onBlur event handler to invoke the validSalesAmt() function when the user
presses the Tab key or clicks the Interest Rate text field. Moving the insertion point to another form field or object
after the down payment value is entered means the text field no longer has the focus, which is called blur. The fol-
lowing step enters the onBlur event handler to call the validSalesAmt() function.
1
onBlur event handler to
activate validSalesAmt()
function when user moves
from DownPayment text field
Scroll down to the
HTML code for the
form and then click
line 127 right after
the closing quote in
the size=”9” attribute
and before the
rightmost /> bracket.
do not press
the e n t e r key
adding onBlur event
handler automatically
wraps; do not press
e n t e r key
Press the
s p a c e b a r
once.
Figure 10-20
onBlur=”validSalesAmt()” to add the event handler to the
DownPayment text field, but do not press the e n t e r key (Figure 10-20).
Type
Validating the Interest Rate and the Number
of Years for the Loan
The CalcLoanAmt() user-defined function is used to validate the interest rate and
number of years of the loan. The code for CalcLoanAmt() is shown in Table 10-14. This
function is called after the user enters the interest rate value, selects the number of years
from the drop-down menu, and then clicks the Calculate button.
Table 10-14 Code to Validate the Interest Rate
Line
Code
function CalcLoanAmt() {
46
loanRate=parseFloat(homeLoanForm.Rate.value)
47
if (isNaN(loanRate) || (loanRate <= 0)) {
48
alert(“The interest rate is not a valid number!”)
49
homeLoanForm.Rate.value = “”
50
homeLoanForm.Rate.focus()
51
}
52
Line 46 declares the user-defined function CalcLoanAmt. This function is called
when the user clicks the Calculate button. Line 47 passes the value in the Interest Rate
text field to the loanRate variable, and converts it to a floating-point number using the
parseFloat() function. Because the interest rate is a floating-point number, you must use
the parseFloat() function to keep the interest rate a floating-point number.
The if statement in line 48 tests the loanRate variable to determine if it is a number
or if the value is less than or equal to zero. If the result of the condition is true, an alert
message (line 49) notifies the user that the interest rate is not valid. Line 50 clears the data
entered in the Interest Rate text field, and then line 51 sets the focus back to the Interest
 
Search WWH ::




Custom Search