check user input for float

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

    #1

    check user input for float

    Hello!

    How do I check user input for following formats:

    I only want to accept floats or integers written as:

    232,45
    232
    242.45
    345


    accepted values should be from
    100 to 30000 (100.00 - 30000.00)

    can anyone help me?

    I only need the code inside the checkForm() funktion. The basics are clear.


    Thank you all!
    Georg


  • Evertjan.

    #2
    Re: check user input for float

    Georg Vassilopulos wrote on 04 jul 2004 in comp.lang.javas cript:
    [color=blue]
    > Hello!
    >
    > How do I check user input for following formats:
    >
    > I only want to accept floats or integers written as:
    >
    > 232,45
    > 232
    > 242.45
    > 345
    >
    >
    > accepted values should be from
    > 100 to 30000 (100.00 - 30000.00)[/color]

    function checkForm(x){
    x = x.replace(/,/.'.')
    if (!/^\d{3,5}(\.\d\d )?$/.test(x)) return false
    x = +x
    if (x<100||x>30000 ) return false
    return true
    }
    [color=blue]
    > I only need the code inside the checkForm() funktion. The basics are
    > clear.[/color]

    Sorry, what basics?


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Dr John Stockton

      #3
      Re: check user input for float

      JRS: In article <cc8s4o$eef$05$ 1@news.t-online.com>, seen in
      news:comp.lang. javascript, Georg Vassilopulos <lupox@NOSPAMwe b.de>
      posted at Sun, 4 Jul 2004 14:11:03 :[color=blue]
      >How do I check user input for following formats:
      >
      >I only want to accept floats or integers written as:
      >
      >232,45
      >232
      >242.45
      >345
      >
      >
      >accepted values should be from
      >100 to 30000 (100.00 - 30000.00)
      >
      >can anyone help me?
      >
      >I only need the code inside the checkForm() funktion. The basics are clear.[/color]

      See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>; I suggest a RegExp

      /^[1-9]\d{2,4}([\.,]\d\d)?$/

      followed by setting var X = + mycontrol.value
      and testing that X <= 3e4 arithmetically.

      Checking the upper limit in the RegExp is possible but not reasonable.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> JL / RC : 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

      • Evertjan.

        #4
        Re: check user input for float

        Dr John Stockton wrote on 04 jul 2004 in comp.lang.javas cript:
        [color=blue]
        > See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>; I suggest a RegExp
        >
        > /^[1-9]\d{2,4}([\.,]\d\d)?$/[/color]

        you need the , by . replace here, I think
        [color=blue]
        > followed by setting var X = + mycontrol.value
        > and testing that X <= 3e4 arithmetically.
        >
        > Checking the upper limit in the RegExp is possible but not reasonable.
        >[/color]



        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress)

        Comment

        • Dr John Stockton

          #5
          Re: check user input for float

          JRS: In article <Tcw4bGCCCF6AFw 4B@merlyn.demon .co.uk>, seen in
          news:comp.lang. javascript, Dr John Stockton <spam@merlyn.de mon.co.uk>
          posted at Sun, 4 Jul 2004 19:46:26 :[color=blue]
          >JRS: In article <cc8s4o$eef$05$ 1@news.t-online.com>, seen in
          >news:comp.lang .javascript, Georg Vassilopulos <lupox@NOSPAMwe b.de>
          >posted at Sun, 4 Jul 2004 14:11:03 :[/color]
          [color=blue][color=green]
          >>I only want to accept floats or integers written as:
          >>
          >>232,45
          >>232
          >>242.45
          >>345
          >>
          >>
          >>accepted values should be from
          >>100 to 30000 (100.00 - 30000.00)
          >>
          >>can anyone help me?
          >>
          >>I only need the code inside the checkForm() funktion. The basics are clear.[/color]
          >
          >See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>; I suggest a RegExp
          >
          > /^[1-9]\d{2,4}([\.,]\d\d)?$/
          >
          >followed by setting var X = + mycontrol.value
          >and testing that X <= 3e4 arithmetically.
          >
          >Checking the upper limit in the RegExp is possible but not reasonable.[/color]

          As Evertjan pointed out, substitute dot for comma at some stage, before
          conversion to number. If your error message shows the original input,
          show that without substitution.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
          Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
          I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
          free, DOS/Win/UNIX, <URL:http://www.idiotsdelig ht.net/minitrue/> Update hope?

          Comment

          Working...