I have a isNumeric function to check a text field is numeric .. I nowneed a isFloat

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bizt

    I have a isNumeric function to check a text field is numeric .. I nowneed a isFloat

    Hi,

    I am using the following function to validate a forms value as an
    integer

    function isNumeric(str){
    var numericExpressi on = /^[0-9]+$/;
    if(str.match(nu mericExpression )){
    return true;
    }else{
    return false;
    }
    }

    I need one to validate a forms value as a float number. I understand
    that it should just simply be a change to the value of the
    numericExpressi on variable but Im not too good with expression. Ive
    tried doing some searches on google but either couldnt find the
    relevant function or they just didnt work. Can anyone help? Thanks

    Burnsy
  • Janwillem Borleffs

    #2
    Re: I have a isNumeric function to check a text field is numeric.. I now need a isFloat

    bizt schreef:
    I need one to validate a forms value as a float number. I understand
    that it should just simply be a change to the value of the
    numericExpressi on variable but Im not too good with expression. Ive
    tried doing some searches on google but either couldnt find the
    relevant function or they just didnt work. Can anyone help? Thanks
    >
    This could do it:

    var numericExpressi on = /^[0-9]+(\.[0-9]+)?$/;


    JW

    Comment

    • SAM

      #3
      Re: I have a isNumeric function to check a text field is numeric.. I now need a isFloat

      bizt a écrit :
      >
      I need one to validate a forms value as a float number.

      function isNum(str) {
      return str==str.match(/^[0-9]*\.?[0-9]*/);
      }

      --
      sm

      Comment

      • Joost Diepenmaat

        #4
        Re: I have a isNumeric function to check a text field is numeric .. I now need a isFloat

        Joost Diepenmaat <joost@zeekat.n lwrites:
        See Ecma 7.8.3 for the DecimalLiteral production.
        Oops: that should be Ecma-262, chapter 7.8.3

        --
        Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

        Comment

        • Dr J R Stockton

          #5
          Re: I have a isNumeric function to check a text field is numeric .. I now need a isFloat

          In comp.lang.javas cript message <24b65960-c3a8-444a-b5a9-9363924a40a9@c6
          5g2000hsa.googl egroups.com>, Wed, 17 Sep 2008 08:37:23, bizt
          <bissatch@yahoo .co.ukposted:
          >I am using the following function to validate a forms value as an
          >integer
          >
          >function isNumeric(str){
          var numericExpressi on = /^[0-9]+$/;
          if(str.match(nu mericExpression )){
          return true;
          }else{
          return false;
          }
          >}

          That shows that the input is a non-empty string of decimal digits. The
          number of possible integer values is one less than twice the number of
          strings that the above allows.
          >I need one to validate a forms value as a float number. I understand
          >that it should just simply be a change to the value of the
          >numericExpress ion variable but Im not too good with expression. Ive
          >tried doing some searches on google but either couldnt find the
          >relevant function or they just didnt work. Can anyone help? Thanks
          All JavaScript Numbers are stored in float format.

          Do you want to consider integers as a subset of floats?

          How about fixed-point numbers? Two replies have provided only tests for
          fixed-point - perhaps they don't know what floating-point really means..
          Should you exclude infinities?

          Should 33.0 be acceptable as an integer?

          For a problem to be reliably solved, it is first necessary for it to be
          accurately defined.

          It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

          --
          (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk IE7 FF2 Op9 Sf3
          news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
          <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • Curtis

            #6
            Re: I have a isNumeric function to check a text field is numeric.. I now need a isFloat

            bizt wrote:
            Hi,
            >
            I am using the following function to validate a forms value as an
            integer
            >
            function isNumeric(str){
            var numericExpressi on = /^[0-9]+$/;
            if(str.match(nu mericExpression )){
            return true;
            }else{
            return false;
            }
            }
            >
            I need one to validate a forms value as a float number. I understand
            that it should just simply be a change to the value of the
            numericExpressi on variable but Im not too good with expression. Ive
            tried doing some searches on google but either couldnt find the
            relevant function or they just didnt work. Can anyone help? Thanks
            >
            Burnsy
            Technically, if something is numeric, it shouldn't matter if it's a
            float or not. However, you might try:

            function isInteger(num) {
            if ( !isNaN(num) ) {
            return parseInt(num) == parseFloat(num) ? true : false;
            }
            else {
            return false;
            }
            }

            function isFloat(num) {
            if ( !isNaN(num) ) {
            if ( /\.0+$/.test(num) ) {
            return true;
            }
            else {
            return parseInt(num) != parseFloat(num) ? true : false;
            }
            }
            else {
            return false;
            }
            }

            Guess I couldn't entirely eliminate regex.

            --
            Curtis

            Comment

            • optimistx

              #7
              Re: I have a isNumeric function to check a text field is numeric .. I now need a isFloat

              Dr J R Stockton wrote:
              For a problem to be reliably solved, it is first necessary for it to
              be accurately defined.
              >
              It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
              Yes, correct. If any poster defines the problem accurately and reads all of
              c.l.j
              (of course understanding everything) and FAQ (incl. ECMAScript-262
              standards),
              then most probably there is no need to ask. This might take some years,
              though.

              And that may be a good thing: no stupid questions here, only perfectly
              well formulated expert questions, which will be answered perfectly and
              reliably, with even all the spaces in correct places as in jslint.com in
              strictest
              mode.



              Comment

              • SAM

                #8
                Re: I have a isNumeric function to check a text field is numeric.. I now need a isFloat

                Joost Diepenmaat a écrit :
                SAM <stephanemoriau x.NoAdmin@wanad oo.fr.invalidwr ites:
                >
                >It was not asked.
                >
                I'm not so sure. The original post does not define what is meant by
                "floating point number" OR "validate".
                Possibly the OP has so much difficulties with english as me ?

                I refer only to proposed function

                function isNumeric(str){
                var numericExpressi on = /^[0-9]+$/;
                if(str.match(nu mericExpression )){
                return true;
                }else{
                return false;
                }
                }

                function isNumeric(str) --true/false

                Understood this function has to be applied on text-fields of a form to
                check if the values could be a number (in normal human design)
                I expect the form is not submited if that find an "error"


                function isNumeric(str) { return str == str*1; }

                --
                sm

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: I have a isNumeric function to check a text field is numeric.. I now need a isFloat

                  Curtis wrote:
                  bizt wrote:
                  >I am using the following function to validate a forms value as an
                  >integer
                  >>
                  >function isNumeric(str){
                  > var numericExpressi on = /^[0-9]+$/;
                  > if(str.match(nu mericExpression )){
                  > return true;
                  > }else{
                  > return false;
                  > }
                  >}
                  >>
                  >I need one to validate a forms value as a float number. I understand
                  >that it should just simply be a change to the value of the
                  >numericExpress ion variable but Im not too good with expression. Ive
                  >tried doing some searches on google but either couldnt find the
                  >relevant function or they just didnt work. Can anyone help? Thanks
                  >
                  Technically, if something is numeric, it shouldn't matter if it's a
                  float or not. However, you might try:
                  >
                  function isInteger(num) {
                  if ( !isNaN(num) ) {
                  return parseInt(num) == parseFloat(num) ? true : false;
                  }
                  else {
                  return false;
                  }
                  }
                  That is not going to provide a correct result. Remember that parseInt()
                  without second argument parses the string based on its prefix: "0x" as
                  hexadecimal, "0" as octal (implementation-dependent; JavaScript 1.8 still
                  does it, though), other numerics as decimal, and non-numeric as NaN -- which
                  is the reason why we usually recommend parseInt(..., 10) in scripts.
                  parseFloat(), on the other hand, parses every value as a decimal value and
                  returns `NaN' if it cannot be interpreted as such. So for example

                  !isNaN("0123")

                  evaluates to `true', but

                  parseInt("0123" ) == parseFloat("012 3")

                  evaluates to `false' (because 83 != 123), although the value clearly can be
                  considered an integer.

                  And finally, the `==' operation results in a boolean value already. The
                  conditional operation is superfluous and inefficient in such a case; that
                  would seem to apply for all programming languages that have it.
                  function isFloat(num) {
                  if ( !isNaN(num) ) {
                  if ( /\.0+$/.test(num) ) {
                  return true;
                  }
                  else {
                  return parseInt(num) != parseFloat(num) ? true : false;
                  }
                  }
                  else {
                  return false;
                  }
                  }
                  Obvious by now, this test is equally flawed. ISTM it can be replaced safely
                  with

                  function getDecimals(num )
                  {
                  return num % 1;
                  }

                  While the return value is of type `number' and not of type `boolean' here,
                  it suffices for implicit type conversion later. Incidentally, it does not
                  make much sense to test an ECMAScript Number value for being a
                  floating-point value, because *all* ECMAScript Number values are IEEE-754
                  double-precision [64-bit] floating-point values.
                  Guess I couldn't entirely eliminate regex.
                  Guess you haven't RTFM, the FAQ, or the Specification.


                  PointedEars
                  --
                  var bugRiddenCrashP ronePieceOfJunk = (
                  navigator.userA gent.indexOf('M SIE 5') != -1
                  && navigator.userA gent.indexOf('M ac') != -1
                  ) // Plone, register_functi on.js:16

                  Comment

                  • Curtis

                    #10
                    Re: I have a isNumeric function to check a text field is numeric.. I now need a isFloat

                    Thomas 'PointedEars' Lahn wrote:
                    Curtis wrote:
                    >bizt wrote:
                    >>I am using the following function to validate a forms value as an
                    >>integer
                    >>>
                    >>function isNumeric(str){
                    >> var numericExpressi on = /^[0-9]+$/;
                    >> if(str.match(nu mericExpression )){
                    >> return true;
                    >> }else{
                    >> return false;
                    >> }
                    >>}
                    >>>
                    >>I need one to validate a forms value as a float number. I understand
                    >>that it should just simply be a change to the value of the
                    >>numericExpres sion variable but Im not too good with expression. Ive
                    >>tried doing some searches on google but either couldnt find the
                    >>relevant function or they just didnt work. Can anyone help? Thanks
                    >Technically, if something is numeric, it shouldn't matter if it's a
                    >float or not. However, you might try:
                    >>
                    >function isInteger(num) {
                    > if ( !isNaN(num) ) {
                    > return parseInt(num) == parseFloat(num) ? true : false;
                    > }
                    > else {
                    > return false;
                    > }
                    >}
                    >
                    That is not going to provide a correct result. Remember that parseInt()
                    without second argument parses the string based on its prefix: "0x" as
                    hexadecimal, "0" as octal (implementation-dependent; JavaScript 1.8 still
                    does it, though), other numerics as decimal, and non-numeric as NaN -- which
                    is the reason why we usually recommend parseInt(..., 10) in scripts.
                    parseFloat(), on the other hand, parses every value as a decimal value and
                    returns `NaN' if it cannot be interpreted as such. So for example
                    >
                    !isNaN("0123")
                    >
                    evaluates to `true', but
                    >
                    parseInt("0123" ) == parseFloat("012 3")
                    >
                    evaluates to `false' (because 83 != 123), although the value clearly can be
                    considered an integer.
                    Yes, thanks, I forgot about that.
                    And finally, the `==' operation results in a boolean value already. The
                    conditional operation is superfluous and inefficient in such a case; that
                    would seem to apply for all programming languages that have it.
                    Yes, I see now, how inefficient I had it.
                    >function isFloat(num) {
                    > if ( !isNaN(num) ) {
                    > if ( /\.0+$/.test(num) ) {
                    > return true;
                    > }
                    > else {
                    > return parseInt(num) != parseFloat(num) ? true : false;
                    > }
                    > }
                    > else {
                    > return false;
                    > }
                    >}
                    >
                    Obvious by now, this test is equally flawed. ISTM it can be replaced safely
                    with
                    >
                    function getDecimals(num )
                    {
                    return num % 1;
                    }
                    Very nice, that's way more elegant, and simpler.
                    While the return value is of type `number' and not of type `boolean' here,
                    it suffices for implicit type conversion later. Incidentally, it does not
                    make much sense to test an ECMAScript Number value for being a
                    floating-point value, because *all* ECMAScript Number values are IEEE-754
                    double-precision [64-bit] floating-point values.
                    >
                    >Guess I couldn't entirely eliminate regex.
                    >
                    Guess you haven't RTFM, the FAQ, or the Specification.
                    I have read the manual, some of the FAQ, but not the spec. I wasn't
                    trying to come off as arrogant or anything, sorry if it sounded that
                    way. Thanks for the correction, I wouldn't want to mislead anyone with
                    an incorrect solution.
                    >
                    PointedEars
                    --
                    Curtis

                    Comment

                    Working...