Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    if else not working, alert boxes

    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.

    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!');
                	}
    And then here is the HTML part of it.

    Code:
    <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>

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Here is your corrected code
    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.
    Software and cathedrals are much the same - first we build them, then we pray.

  • #3
    New Coder
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

    Code:
    }
    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

  • #4
    New Coder
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    nevermind i figured that part out, seemed to be a minor error! thanks for the help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •