Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <meta charset="UTF-8">
- <head>
- <script language = "javascript" type = "text/javascript">
- <!--
- function validateForm()
- {
- var tableNumber = Number(document.tableForm.table_number.value);
- var subtotal = Number(document.tableForm.subtotal.value);
- if(tableNumber == "" || tableNumber < 1 || tableNumber > 36)
- {
- alert("Table number cannot be blank, and must be between 1 and 36, inclusive.");
- document.tableForm.table_number.focus();
- }
- else if(subtotal == "" || subtotal <= 0)
- {
- alert("Subtotal must be a non-negative integer greater than zero. Please enter a valid value.");
- document.tableForm.subtotal.focus();
- }
- else
- {
- // The general means of rounding to a specified decimal place in javascript is as follows:
- // a) multiply the number to be rounded by 10^x power,
- // where x is the decimal place to be rounded to.
- // b) Apply Math.round() to the result.
- // c) Divide the result of this by 10^x.
- // (from www.javascriptkit.com/javatutors/round.shtml)
- var tax = (Math.round(subtotal * 0.1 * 10**2))/10**2;
- var total = subtotal + tax;
- document.write("<b>Table number: </b>" + tableNumber + "<br />");
- document.write("The subtotal was $" + subtotal.toFixed(2) + "<br />");
- document.write("The tax was $" + tax + "<br />");
- document.write("The total was $" + total.toFixed(2) + "<br />");
- }
- }
- //-->
- </script>
- </head>
- <body>
- <h1>Tables</h1>
- <hr />
- <form name="tableForm" class="inTable">
- Table number: <input type="text" name="table_number"><br />
- Subtotal: <input type="text" name="subtotal">
- <br />
- <input type="button" value="Submit" onclick="validateForm()">
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement