<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

Dani AI

Generated

Missing isFloat was the original problem (as noted). A quick dot-check (also from ) is convenient but unsafe: it returns true for non-numeric strings like abc.def and misses other formats such as scientific notation. improved things by ensuring the string is numeric and contains a dot, which helps, but several edge cases remain: leading/trailing spaces, 1e3, 1.0 (semantics: is that a "float" or an integer?), NaN/Infinity, 0x10, and parseFloat’s partial parsing (parseFloat("1.2abc") === 1.2).

Decide what “float” means for your use case:

  • syntactic float: the string contains a decimal point or exponent (useful for form input detection), or
  • numeric non-integer: the value, when parsed, is not an integer (useful for numeric logic).

Below are two helpers (strict full-string numeric test + the two interpretations). These avoid partial parsing and accept exponent notation:

// full-string numeric test (integers, decimals, exponent)
function isNumericStrict(s) {
  if (typeof s !== 'string') s = String(s);
  s = s.trim();
  if (s === '') return false;
  if (/^[+-]?(?:Infinity|NaN)$/i.test(s)) return false;
  var numericRe = /^[+-]?(?:(?:\d*\.\d+)|(?:\d+\.\d*)|\d+)(?:[eE][+-]?\d+)?$/;
  return numericRe.test(s);
}

function isFloatValue(s) { // numeric non-integer
  if (!isNumericStrict(s)) return false;
  var n = Number(s);
  if (!isFinite(n)) return false;
  return n % 1 !== 0;
}

If you prefer the syntactic check (string shows decimal/exponent), use:

function isFloatFormat(s) {
  return isNumericStrict(s) && /[.eE]/.test(s);
}

Usage note: prefer Number() + anchored regex for validation; avoid parseFloat for strict checks. For simple UI validation, HTML5 <input type="number" step="any"> and the constraint validation API can also help. The blog link mentioned by has other cross-browser approaches if needed.

Recommended Answers

All 5 Replies

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.