check user input for float

Discussion in 'Javascript' started by Georg Vassilopulos, Jul 4, 2004.

  1. 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
    Georg Vassilopulos, Jul 4, 2004
    #1
    1. Advertising

  2. Georg Vassilopulos

    Evertjan. Guest

    Georg Vassilopulos wrote on 04 jul 2004 in comp.lang.javascript:

    > 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)


    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
    }

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


    Sorry, what basics?


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Jul 4, 2004
    #2
    1. Advertising

  3. JRS: In article <cc8s4o$eef$05$-online.com>, seen in
    news:comp.lang.javascript, Georg Vassilopulos <>
    posted at Sun, 4 Jul 2004 14:11:03 :
    >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.


    See <URL:http://www.merlyn.demon.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.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Jul 4, 2004
    #3
  4. Georg Vassilopulos

    Evertjan. Guest

    Dr John Stockton wrote on 04 jul 2004 in comp.lang.javascript:

    > See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>; I suggest a RegExp
    >
    > /^[1-9]\d{2,4}([\.,]\d\d)?$/


    you need the , by . replace here, I think

    > 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.
    >




    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Jul 5, 2004
    #4
  5. JRS: In article <>, seen in
    news:comp.lang.javascript, Dr John Stockton <>
    posted at Sun, 4 Jul 2004 19:46:26 :
    >JRS: In article <cc8s4o$eef$05$-online.com>, seen in
    >news:comp.lang.javascript, Georg Vassilopulos <>
    >posted at Sun, 4 Jul 2004 14:11:03 :


    >>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.

    >
    >See <URL:http://www.merlyn.demon.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.


    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.demon.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.idiotsdelight.net/minitrue/> Update hope?
    Dr John Stockton, Jul 5, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,238
    Roedy Green
    May 10, 2004
  2. kt
    Replies:
    3
    Views:
    40,165
    Betty
    Apr 22, 2005
  3. Jim West
    Replies:
    4
    Views:
    1,400
    Jeff Schwab
    Jan 16, 2004
  4. bd
    Replies:
    0
    Views:
    605
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,505
    James Kanze
    Oct 8, 2009
Loading...

Share This Page