CSCI 4230
Internet Applicaiton Development
Fall 2000
Suugested Solution to Homework #6

(1)    For example:

<html>
  <head>
  <script language="JavaScript">
  <!-- Hide from JavaScript-Impaired Browsers
  function isDigit(c) {
       return ((c >= "0") && (c <= "9"));
  }

  function isFloat(s){
      var i;
   var decimalEncountered = false;
      for (i = 0; i < s.length; i++) {
          if (!isDigit(s.charAt(i)) && (s.charAt(i) != '.')) return false;
    if (decimalEncountered && s.charAt(i) == '.') return false;
    if (s.charAt(i) == '.') decimalEncountered = true;
      }
   return true;
  }

  function showSquare(f, s) {
   if (s == '') {
   f.square.value = "";
   return;
   }
   if (!isFloat(s))
       f.square.value = "Sorry, " + s + " is not a decimal number."
   else {
      val = parseFloat(s);
   f.square.value = val * val;
   }
  }

  // End Hiding -->
  </script>
  </head>
  <body bgcolor="#ccccff">
  <h2>Get the square of a number</h2>
  <form onSubmit="showSquare(this, this.number.value); return false;">
  Please input a non-negative decimal number:
  <input type="text" NAME="number" size="40" value=""
         onChange="showSquare(this.form, this.value);"><br>
  Result Square: <input type="txt" name="square" size="40">
  <br>
  <input type="submit" value="Get Square!">
  </body>
</html>