Advertisement
Vita_Harvey

SOTD_006b

Apr 21st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <meta charset="UTF-8">
  4.     <head>
  5.         <script language = "javascript" type = "text/javascript">
  6.             <!--
  7.  
  8.  
  9.             function validateForm()
  10.             {
  11.                 var tableNumber = Number(document.tableForm.table_number.value);
  12.                 var subtotal = Number(document.tableForm.subtotal.value);
  13.                 if(tableNumber == "" || tableNumber < 1 || tableNumber > 36)
  14.                 {
  15.                     alert("Table number cannot be blank, and must be between 1 and 36, inclusive.");
  16.                     document.tableForm.table_number.focus();
  17.                 }
  18.                 else if(subtotal == "" || subtotal <= 0)
  19.                 {
  20.                     alert("Subtotal must be a non-negative integer greater than zero.  Please enter a valid value.");
  21.                     document.tableForm.subtotal.focus();
  22.                 }
  23.                 else
  24.                 {
  25.                     // The general means of rounding to a specified decimal place in javascript is as follows:
  26.  
  27.                     // a) multiply the number to be rounded by 10^x power,
  28.                     //      where x is the decimal place to be rounded to.
  29.                     // b) Apply Math.round() to the result.
  30.                     // c) Divide the result of this by 10^x.
  31.                     // (from www.javascriptkit.com/javatutors/round.shtml)
  32.                     var tax = (Math.round(subtotal * 0.1 * 10**2))/10**2;
  33.                     var total = subtotal + tax;
  34.                     document.write("<b>Table number: </b>" + tableNumber + "<br />");
  35.                     document.write("The subtotal was $" + subtotal.toFixed(2) + "<br />");
  36.                     document.write("The tax was $" + tax + "<br />");
  37.                     document.write("The total was $" + total.toFixed(2) + "<br />");
  38.                 }
  39.             }
  40.             //-->
  41.         </script>
  42.     </head>
  43.     <body>
  44.         <h1>Tables</h1>
  45.         <hr />
  46.         <form name="tableForm" class="inTable">
  47.             Table number: <input type="text" name="table_number"><br />
  48.             Subtotal: <input type="text" name="subtotal">
  49.             <br />
  50.             <input type="button" value="Submit" onclick="validateForm()">
  51.         </form>
  52.     </body>
  53. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement