Checking for a decimal

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

    #1

    Checking for a decimal


    Hi - can anyone give me a sample of how I would ensure a value in a text
    box was a decimal (it is used for entering money - so has to check for
    no more than 2 decimal places, but still allow should no decimals be
    input) - eg.

    23 - ok
    23.01 - ok
    23.1 - ok
    23.111 - not valid

    Thanks for any help,

    Mark


    *** Sent via Devdex http://www.devdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Flurk

    #2
    Re: Checking for a decimal

    Mark wrote:[color=blue]
    > Hi - can anyone give me a sample of how I would ensure a value in a
    > text box was a decimal (it is used for entering money - so has to
    > check for no more than 2 decimal places, but still allow should no
    > decimals be input) - eg.
    >
    > 23 - ok
    > 23.01 - ok
    > 23.1 - ok
    > 23.111 - not valid[/color]

    Use the regular expression ^(\d+\.\d{0,2}| \d+)$

    This is how you validate your form:

    <form onsubmit="retur n /^(\d+\.\d{0,2}| \d+)$/.test(this.mynu mber.value);">
    <input type="text" name="mynumber" >
    <input type="submit">
    </form>


    --

    Flurk


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Checking for a decimal

      Mark <anonymous@devd ex.com> writes:
      [color=blue]
      > Hi - can anyone give me a sample of how I would ensure a value in a text
      > box was a decimal (it is used for entering money - so has to check for
      > no more than 2 decimal places, but still allow should no decimals be
      > input) - eg.
      >
      > 23 - ok
      > 23.01 - ok
      > 23.1 - ok
      > 23.111 - not valid[/color]

      Is "23." valid? I guess not.

      A simple method that tests for the correct number of digits:

      function(value) {
      return /^\d+(\.\d{1,2}) ?$/.exec(value);
      }

      What do you want to do if the test fails?


      You can also use the more heavyhanded approach of enforcing the format
      whenever the user changes the content:

      <input type="text" ...
      onchange="var match = /^([-+]?\d+)(\.\d*)?$/.exec(this.valu e);
      if (!match) {
      this.value = ''; // whatever input was completely wrong.
      } else if (match[2]) {
      var decimals = match[2].substring(0,3) ;
      decimals += '00'.substring( decimals.length-1);
      this.value = match[1]+decimals;
      }">

      Completely removing the content on a non-number input might be a little
      too rough. It allows prefixed signs, unlike the first test. (Should it?)
      It truncates extra digits, it doesn't round.

      Good luck
      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Mick White

        #4
        Re: Checking for a decimal

        Flurk wrote:
        [color=blue]
        >
        > Use the regular expression ^(\d+\.\d{0,2}| \d+)$
        >
        > This is how you validate your form:
        >
        > <form onsubmit="retur n /^(\d+\.\d{0,2}| \d+)$/.test(this.mynu mber.value);">
        > <input type="text" name="mynumber" >
        > <input type="submit">
        > </form>
        >[/color]


        That would validate "23."
        Mick

        Comment

        • Dr John Stockton

          #5
          Re: Checking for a decimal

          JRS: In article <40cac66e$0$254 79$c397aba@news .newsgroups.ws> , seen in
          news:comp.lang. javascript, Mark <anonymous@devd ex.com> posted at Sat, 12
          Jun 2004 09:01:34 :[color=blue]
          >
          >Hi - can anyone give me a sample of how I would ensure a value in a text
          >box was a decimal (it is used for entering money - so has to check for
          >no more than 2 decimal places, but still allow should no decimals be
          >input) - eg.
          >
          >23 - ok
          >23.01 - ok
          >23.1 - ok
          >23.111 - not valid
          >
          >Thanks for any help,[/color]

          [color=blue]
          >*** Sent via Devdex http://www.devdex.com ***
          >Don't just participate in USENET...get rewarded for it![/color]

          How do you propose, then, to reward us for our assistance?

          The third case should NOT be allowed, since it could be intended to mean
          either 23.01 or 23.10 - proper money format, in most countries, is an
          integer optionally followed by (point digit digit).

          The point is adequately explained in p.19 of the free booklet "your
          guide to decimal money" - in which Lord Fiske asserts that copies are
          being delivered to every household - published by HMSO about 1970.

          The basic RegExp is of the form /^\d+(\.\d\d)?$/

          See also <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          Working...