Regular Expressions and Form Handling

Objectives

In-class Demonstration

BMI.html

<html> <head> <title>Body Mass Index Calculator</title> <script type="text/javascript"> function $(id) { return document.getElementById(id); } function isInteger(s) { return /^[+-]?\d+$/.test(s); } function createDetails() { var f = document.forms[0]; var feet = $('feet').value; var inches = $('inches').value; var weight = $('weight').value; var errmsg = ""; // validate fields if (!isInteger(feet)) errmsg += "Invalid number for feet\n"; else { feet = parseInt(feet); if (feet<=0) errmsg += "Feet must be greater than 0\n"; if (feet>=10) errmsg += "Feet must be less than 10\n"; } if (!isInteger(inches)) errmsg += "Invalid number for inches\n"; else { inches = parseInt(inches); if (inches<0) errmsg += "Inches must be greater than or equal to 0\n"; if (inches>=12) errmsg += "Inches must be less than 12\n"; } if (!isInteger(weight)) errmsg += "Invalid number for weight\n"; else { weight = parseInt(weight); if (weight<=0) errmsg += "Weight must be greater than 0\n"; } // check for errors if (errmsg != "") { alert(errmsg); return false; } // calculate body mass index var totInches = feet * 12 + inches; var bmi = (weight/(totInches*totInches)) * 703; var strBMI = bmi.toFixed(1); // open window and display results var win = window.open("", "_blank", "width=250,height=300,status=no"); win.document.writeln('<html><head><title>BMI Details</title>'); win.document.writeln('<style type="text/css">'); win.document.writeln('td, th { text-align: right; }'); win.document.writeln('</style>'); win.document.writeln('</head>'); win.document.writeln('<body>'); win.document.writeln('<h3>BMI Details</h3>'); win.document.writeln('<p>Feet: ' + feet + '</p>'); win.document.writeln('<p>Inches: ' + inches + '</p>'); win.document.writeln('<p>Weight: ' + weight + '</p>'); win.document.writeln('<p>Body Mass Index: ' + strBMI + '</p>'); var status = "overweight"; if (bmi<25) status = "normal"; if (bmi>30) status = "obesity"; win.document.writeln('<p>A BMI of ' + strBMI + ' indicates ' + status + '</p>'); win.document.writeln('</body></html>'); win.document.close(); return false; } </script> </head> <body> <h3>BMI Calculator</h3> <form action="BMI.php" method="get" id="bmiform" onsubmit="return createDetails();"> <table border="0"> <tr> <td><label for="feet">Feet</label></td> <td><label for="inches">Inches</label></td> <td><label for="weight">Weight</label></td> </tr> <tr> <td><input type="text" size="20" id="feet" name="feet" /></td> <td><input type="text" size="20" id="inches" name="inches" /></td> <td><input type="text" size="20" id="weight" name="weight" /></td> </tr> <tr> <td colspan="3"> <input type="reset" value="Clear" />         <input type="submit" value="Calc" /> </td> </tr> </table> </form> </body> </html>

BMI.php

<html> <head> <title>BMI Details</title> </head> <body> <h3>BMI Details</h3> <?php function isInteger($n) { if (!is_numeric($n)) return false; $float_n = (float) $n; $int_n = (int) $n; return $float_n == $int_n; } $feet = $_GET['feet']; $inches = $_GET['inches']; $weight = $_GET['weight']; $errMessage = ""; // validate fields if (!isInteger($feet)) { $errMessage = $errMessage . "Invalid number for feet<br />"; } else { $feet = (int) $feet; if ($feet < 0) $errMessage = $errMessage . "Feet not allowed to be less than 0<br />"; else if ($feet > 10) $errMessage = $errMessage . "Feet not allowed to be greater than 10<br />"; } if (!isInteger($inches)) { $errMessage = $errMessage . "Invalid number for inches<br />"; } else { $inches = (int) $inches; if ($inches < 0) $errMessage = $errMessage . "Inches not allowed to be less than 0<br />"; else if ($inches >= 12) $errMessage = $errMessage . "Inches not allowed to be greater than or equal to 12<br />"; } if (!isInteger($weight)) { $errMessage = $errMessage . "Invalid number for weight<br />"; } else { $weight = (int) $weight; if ($weight <= 0) $errMessage = $errMessage . "Weight not allowed to be less than or equal to 0<br />"; } if ($inches==0 && $feet==0) { $errMessage = $errMessage . "Total height must be greater than 0<br />"; } echo "<p>Feet: " . $feet . "</p>\n"; echo "<p>Inches: " . $inches . "</p>\n"; echo "<p>Weight: " . $weight . "</p>\n"; // not valid - display error and exit if (strlen($errMessage) != 0) { echo "<p>" . $errMessage . "</p>"; } else { // calculate BMI $totInches = $feet * 12 + $inches; $bmi = ($weight / ($totInches*$totInches)) * 703; $status = "overweight"; if ($bmi < 25) { $status = "normal"; } else if ($bmi > 30) { $status = "obesity"; } echo "<p>A BMI of " . sprintf("%.1f", $bmi) . " indicates " . $status . "</p>\n"; } ?> </body> </html>