Can\'t even get it to calculate now.....I was trying to e nhance the data valida
ID: 3550956 • Letter: C
Question
Can't even get it to calculate now.....I was trying to enhance the data validation code so it displays the error messages in the span elements that follow the text boxes. These messages should be removed when the related fields are valid. Do this one field at a time, starting from the top.
var $ = function (id) {
return document.getElementById(id);
}
var calculateClick = function () {
var investment = parseFloat( $("investment").value );
var annualRate = parseFloat( $("rate").value );
var years = parseInt( $("years").value );
}
var investmentError=document.getElementById("investment_error");
var rateError=document.getElementById("rate_error");
var yearsError=document.getElementById("years_error");
if (isNaN(investment) || investment < 100 || investment > 100000) {
$("investment_error").firstChild.nodeValue = ("Must be an integer from 100 - 100,000.");
isValid = false;
}
else {
$("investment_error").firstChild.nodeValue = "";
}
else if (isNaN(annualRate) || annualRate < .1 || annualRate > 12) {
$("rate_error").firstChild.nodeValue = ("Must be a value from .1 - 12.");
isValid = false;
} else {
$("rate_error").firstChild.nodeValue = "";
}
else if(isNaN(years) || years < 1 || years > 50) {
$("years_error").firstChild.nodeValue = ("Must be an integer from 1 - 50.");
isValid = false;
} else {
$("years_error").firstChild.nodeValue = "";
}
// if all entries are valid, calulate future value
else {
futureValue = investment;
for ( i = 12; i <= years; i++ ) {
futureValue += futureValue * annualRate / 100;
}
$("future_value").value = futureValue.toFixed();
}
}
var clear = function () {
$("rate").value = "";
}
window.onload = function () {
$("calculate").onclick = calculateClick;
$("investment").focus();
$("rate").ondblclick = clear;
}
Explanation / Answer
I have edited the html file a bit.
Here is the link for the test.html file: https://gist.github.com/bitwiser/9192598
Here is the link for the script.js file : https://gist.github.com/bitwiser/9191406
Hope it helps.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.