<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

The isFloat function is missing.


Matti Ressler
Suomedia

can u help me to solve isFloat, pls.

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);
    }
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.