<html>
<head>
<script type="text/javascript">
function validateFloat()
   {
      var o = document.frmInput.txtInput;
      switch (isFloat(o.value))
      {
         case true:
            alert(o.value + " is an float")
            break;
         case false:
            alert(o.value + " is not an float")
      }
   }

   </script>
</head>
<body>
<form name="frmInput">
   Enter something: <input name="txtInput" size="4">
   <input type="button" value="Validate" onclick="validateFloat()"></input>
</form>
</body>
</html>

Not working. pls help

This is rather simple (eg, it will return a.b as a float):

function isFloat(value) {
            if (/\./.test(value)) {

                return true;

            } else {

                return false;

            }
          }

Google is your friend to find a more comprehensive float checking function.


Matti Ressler
Suomedia

A thorough way of doing it would be:

/**
 * Checks if a given string is float or not
 *
 * @author sos
 * @param {String} val The string in question.
 */
  function isFloat(val) {
    if(!val || (typeof val != "string" || val.constructor != String)) {
      return(false);
    }
    var isNumber = !isNaN(new Number(val));
    if(isNumber) {
      if(val.indexOf('.') != -1) {
        return(true);
      } else {
        return(false);
      }
    } else {
      return(false);
    }
  }
This article has been dead for over six months. Start a new discussion instead.