HTML and CSS Reference
In-Depth Information
Lines 19 through 22 declare the variables that will be used in the two user-defined
functions. Table 10-13 shows the code to enter the validSalesAmt() user-defined function
and the statements necessary to validate and calculate the mortgage loan amount based
on the sales amount minus the down payment amount using the parseInt() and isNaN()
functions.
Table 10-13 Code for validSalesAmt() Function to Validate and Calculate the Loan Amount
Line
Code
24
function validSalesAmt() {
25
var salesAmt=parseInt(homeLoanForm.SaleAmount.value,10)
26
if (isNaN(salesAmt) || (salesAmt <= 0)) {
27
alert("The sales price is not a valid number!")
28
homeLoanForm.SaleAmount.value = ""
29
homeLoanForm.SaleAmount.focus()
30
}
31
else {
32
var downPmtAmt=parseInt(homeLoanForm.DownPayment.value,10)
33
if (isNaN(downPmtAmt) || (downPmtAmt <= 0) || (downPmtAmt > salesAmt)) {
34
alert("The down payment should be greater than 0 and less than the sales amount!")
35
homeLoanForm.DownPayment.value = ""
36
homeLoanForm.DownPayment.focus()
37
}
38
else {
39
loanAmt = salesAmt-downPmtAmt
40
homeLoanForm.LoanAmount.value = loanAmt
41
homeLoanForm.Rate.focus()
42
}
43
}
44
}
Line 24 declares the validSalesAmt() function and uses the values entered or
selected in the LoanForm. Line 25 assigns the data entered in the SaleAmount text field
to the salesAmt variable and the parseInt() function converts that value to an integer.
The if statement beginning on line 26 tests if the value for the salesAmt variable is not a
number or if the value entered is less than or equal to zero. If the result of the condition
is true (that is, the value entered is not a number or it is a negative number), the function
notifies the user with an alert message (line 27) that the sales amount is not valid, clears
the data entered in the SaleAmount text field (line 28), and then sets the focus back to the
SaleAmount text field (line 29). The brace in line 30 closes the if statement.
Because an event must be executed until completion, the validSalesAmt() function
validates all the entered values. Line 31 continues with the else statement to validate the
down payment amount. Line 32 converts the DownPayment value to an integer assigned
the downPmtAmt variable. The if statement at line 33 tests if the downPmtAmt is a num-
ber and greater than zero but less than or equal to the salesAmt. If any of the compound
conditions is false, line 34 displays the alert error message. Line 35 clears the input text
field, and line 36 sets the focus back to the DownPayment input text field.
Search WWH ::




Custom Search