DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   JavaScript / DHTML / AJAX (http://www.daniweb.com/forums/forum117.html)
-   -   Float value checking (http://www.daniweb.com/forums/thread115310.html)

lordx78 Mar 23rd, 2008 6:10 am
Float value checking
 
<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

Suomedia Mar 23rd, 2008 2:37 pm
Re: Float value checking
 
The isFloat function is missing.


Matti Ressler
Suomedia

lordx78 Mar 24th, 2008 7:10 am
Re: Float value checking
 
can u help me to solve isFloat, pls.

Suomedia Mar 24th, 2008 12:13 pm
Re: Float value checking
 
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

~s.o.s~ Mar 30th, 2008 2:22 pm
Re: Float value checking
 
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);
    }
  }


All times are GMT -4. The time now is 3:23 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC