Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Simple Calculator</title>
- <style type="text/css">
- <!--
- #container {
- font-family: Verdana, Geneva, sans-serif;
- font-size: 100%;
- width: 570px;
- margin: 0 auto;
- }
- #msg {
- width:90%;
- color:#036;
- font-weight:bold;
- margin-left:20px;
- }
- body
- {
- font-family:Verdana, Geneva, sans-serif;
- font-size:100%;
- background-color:#036;
- }
- form {
- background-color:#FFF;
- margin-top: 25px;
- padding:10px;
- width:95%;
- border:2px solid red;
- }
- label {
- display:block; /*forces the label to an entire line, allowing the textbox to fall below the label*/
- font-weight:bold;
- margin-left:10px;
- font-size:80%;
- }
- input, p {
- margin-left:10px;
- }
- -->
- </style>
- <script language="javascript" type="text/javascript">
- function preCalc()
- {
- var valx = parseFloat(document.form1.valueone.value);
- var valy = parseFloat(document.form1.valuetwo.value);
- var radGp = document.form1.operation;
- var radioLength = radGp.length;
- var chkValue;
- var answer;
- for(var i = 0; i < radioLength; i++) {
- if(radGp[i].checked) {
- chkValue = radGp[i].value;
- }
- }
- var msg = document.getElementById("msg");
- if(isNaN(valx) && isNaN(valy))
- {
- msg.style.color = "red";
- msg.innerHTML = "Values 1 and 2 must be numbers";
- }
- else
- {
- answer = calculate(valx, valy, chkValue);
- if(!isNaN(answer))
- {
- msg.style.color = "red";
- msg.innerHTML = "Result: " + answer;
- }
- else
- {
- msg.style.color = "red";
- msg.innerHTML = answer;
- }
- }
- }
- function calculate(x, y, op)
- {
- var z;
- switch (op)
- {
- case "add":
- z= x+y;
- break;
- case "sub":
- z=x-y;
- break;
- case "mul":
- z=x*y;
- break;
- case "div":
- z=x/y;
- break;
- case "mod":
- z=x%y;
- break;
- default:
- z="You did not select an operation";
- }
- return z;
- }
- </script>
- </head>
- <body>
- <div id="container">
- <form id="form1" name="form1" method="post" action="">
- <p>
- <label for="valueone">Value 1: </label>
- <input type="text" name="valueone" id="valueone" />
- </p>
- <p>
- <label for="valuetwo">Value 2: </label>
- <input type="text" name="valuetwo" id="valuetwo" />
- </p>
- <p>
- <label>
- <input type="radio" name="operation" value="add" id="operation_0" />
- Add (+)</label>
- <br />
- <label>
- <input type="radio" name="operation" value="sub" id="operation_1" />
- Subtract (-)</label>
- <br />
- <label>
- <input type="radio" name="operation" value="mul" id="operation_2" />
- Multiply (*)</label>
- <br />
- <label>
- <input type="radio" name="operation" value="div" id="operation_3" />
- Divide (/)</label>
- <br />
- <label>
- <input type="radio" name="operation" value="mod" id="operation_4" />
- Modulus (%)</label>
- <br />
- <input type="button" name="button" id="button" value="Calculate" onclick="preCalc()"/>
- <input type="reset" name="clrForm" id="clrForm" value="Clear" />
- </p><span id="msg">Results</span>
- </form>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment