...

View Full Version : if else not working, alert boxes



I2izzo
03-21-2012, 03:58 PM
Working on a project and I am trying to get error messages to pop up when either a number wasn't entered or another error message if nothing was entered. otherwise if a number was probably entered it would perform the proper function, heres where I am at, does anyone know what is wrong with it? it wont calculate or even throw up a pop up box.


<script language="JavaScript" type="text/javascript">

function check_numbers(){

var sal = document.temp_form.num3.value;
var error_message = "";

if (sal == "")
{
error_message += "You must enter a value gross annual salary \n";
}
else if (isNaN(sal))
error_message += "Value entered is not a number, please try again. \n";
}
if (error_message != "")
{
alert ("Please correct the following errors: \n_________________________________\n\n" + error_message);
}
else{
Calculate( parseInt(sal))
}
}
function calculate()
{
var sal = Number(document.getElementById('num3').value);
var fName = document.getElementById('num1').value;
var lname = document.getElementById('num2').value;
alert('Hello '+ fName + ' '+ lname + ', you would pay $'+ sal * .2 + ' in Federal taxes and $'+ sal * .1 + ' in State taxes, leaving you $'+ sal * .7 + ' to take home annually!');
}

And then here is the HTML part of it.



<b>
Here is a pay slip generator that will determine your net annual salary.
<br>
Simply type in your first and last name along with gross salary and hit calculate.
<br>
Doing this will let you know how much you will end up paying in both Federal and State taxes.
</b>
<form action="" method="post" name="temp_form">
<p> Enter your First Name:
<input name="num1" type="text" id="num1" size="10" maxlength="10">
</p>
<p> Enter your Last Name:
<input name="num2" type="text" id="num2" size="10" maxlength="10">
</p>
<p> Enter your gross salary:
<input name="num3" type="text" id="num3" size="10" maxlength="10">
</p>

Click this button to calulate your annual net pay!
<input type="submit" onclick="check_numbers()" value="Generate Pay Slip" />
<input type="button" value="Reset Form" onClick="this.form.reset()" />
</FORM>
</body>
</html>

ckeyrouz
03-21-2012, 04:15 PM
Here is your corrected code


<script language="JavaScript" type="text/javascript">

function check_numbers(){

var sal = document.temp_form.num3.value;
var error_message = "";

if (sal == "")
{
error_message += "You must enter a value gross annual salary \n";
}
else if (isNaN(sal))

{
error_message += "Value entered is not a number, please try again. \n";
}
if (error_message != "")
{
alert ("Please correct the following errors: \n_________________________________\n\n" + error_message);
}
else{
calculate( parseInt(sal))
}
}
function calculate()
{
var sal = Number(document.getElementById('num3').value);
var fName = document.getElementById('num1').value;
var lname = document.getElementById('num2').value;
alert('Hello '+ fName + ' '+ lname + ', you would pay $'+ sal * .2 + ' in Federal taxes and $'+ sal * .1 + ' in State taxes, leaving you $'+ sal * .7 + ' to take home annually!');
}


The two errors are marked in red:
1- There is a missing curly bracket after else if (isNaN(sal))
2- you are calling the function calculate with a capital C while it is defined with a small c.

I2izzo
03-21-2012, 04:37 PM
wow... i cant believe that was it, works great after those minor adjustments. now im trying to tweak the function calculate so it will not only pull an alert box but display it on a redirected page.. so im working on adding a document.write function and cant figure this one out either!


}
function calculate()
{
var sal = Number(document.getElementById('num3').value);
var fName = document.getElementById('num1').value;
var lname = document.getElementById('num2').value;
alert('Hello '+ fName + ' '+ lname + ', you would pay $'+ sal * .2 + ' in Federal taxes and $'+ sal * .1 + ' in State taxes, leaving you $'+ sal * .7 + ' to take home annually!');
document.write ('Hello '+ fName + ' '+ lname + ', you would pay $'+ sal * .2 + ' in Federal taxes and $'+ sal * .1 + ' in State taxes, leaving you $'+ sal * .7 + ' to take home annually!');
document.write("<a href=JavaScript:history.back();>Click here to go back</a>");
}

After trying to click the button with those additions it will do nothing, botched the script! thank you in advance, really helpful info

I2izzo
03-21-2012, 04:42 PM
nevermind i figured that part out, seemed to be a minor error! thanks for the help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum