Web Analytics Made Easy -
StatCounter Only allow multiples of 5 in a text box or round to nearest multiple - CodingForum

Announcement

Collapse
No announcement yet.

Only allow multiples of 5 in a text box or round to nearest multiple

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Only allow multiples of 5 in a text box or round to nearest multiple

    Hi there,

    I'm creating a large order form and need to limit the input to multiples of 5.

    Is there a way that JavaScript can automatically round a number up to the nearest multiple? i.e. user enters 8, on pressing tab to next text box, value changes to 10?

    Thanks in advance....

  • #2
    Use this, for example:

    <input type="text" onblur="this.value=Math.round(parseInt(this.value)/5)*5" />
    www.united-scripts.com
    www.codebattles.org

    Comment


    • #3
      thanks mate, works perfectly....

      Comment


      • #4
        should've thanked also to the mates on the other forum... They gave you also similar solutions...
        KOR
        Offshore programming
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment


        • #5
          you should use ceil not round if you want to always round up as you say

          Comment


          • #6
            you should use ceil not round if you want to always round up as you say
            Nope. He wants to round to the nearest 5 multiple, ceil or floor.

            Math.ceil would have round it always to the nearest down value
            KOR
            Offshore programming
            -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

            Comment


            • #7
              aaa. I mean up value... If it is ceil... yea
              KOR
              Offshore programming
              -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

              Comment


              • #8
                Hi there,

                I'm creating a large order form and need to limit the input to multiples of 5.

                Is there a way that JavaScript can automatically round a number up to the nearest multiple? i.e. user enters 8, on pressing tab to next text box, value changes to 10?

                Thanks in advance....

                Comment


                • #9
                  But the thread subject is...

                  Only allow multiples of 5 in a text box or round to nearest multiple
                  So it means round up or down depending on the nearest multiple.

                  i.e.
                  8 => 10 //round up
                  7 => 5 //round down

                  Therefore, the code that piz suggested works perfectly.
                  Glenn
                  vBulletin Mods That Rock!

                  Comment


                  • #10
                    Even my English is not my first language I think the missunderstanding came from a bad translation...

                    As far as I know, to round up is a verbal construction in itself, where up has not the sense of direction. I.e : to shut up, to give up... To round up means simply to round.

                    Further more, when he mentioned the nearest multiple I think that is clear he need to simply round, not to round ceil it.
                    KOR
                    Offshore programming
                    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                    Comment


                    • #11
                      k I ain't english either . but to me the only logical interpretation of:
                      round a number up to the nearest multiple.

                      would be that any and all number where rounded up.

                      where as if direction was of no matter it would be:
                      round a number to the nearest multiple.

                      and down would be.
                      round a number down to the nearest multiple

                      if a word of direction had no meaning how would you ever tell people what way you wanted to round a number?.

                      Comment


                      • #12
                        this Forum will become a linguistic one.

                        I presume that, if he wanted up or down he would have specified thus:

                        the nearest up multiple...

                        Interesting is that we are debating thinly linguistic issues, and the initiator never entered and told us what we really ment by that
                        KOR
                        Offshore programming
                        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                        Comment


                        • #13
                          Originally posted by Garadon
                          where as if direction was of no matter it would be:
                          round a number to the nearest multiple.
                          That's exactly what the thread subject said.
                          The first post's message body may have confused you but the subject clearly stated that.

                          The thread starter already confirmed the correctness of the posted script so the real intention is to round up or down to the nearest multiple of 5.
                          Glenn
                          vBulletin Mods That Rock!

                          Comment


                          • #14
                            Thanks to everybody that has replied to my original post…
                            English is my second language [I'm from New Zealand [that's a poor joke]].

                            In my original post I did mean round UP to the next multiple, and so I’m using the math.ceil instead of math.round.

                            The only problem is that when I don’t have a value in a text box and tab across it, I’m left with NaN. I’m using some other JavaScript to prevent users entering non-numeric data, but still getting the NaN when tabbed across.

                            So now my page is looking something like this: [any idea how to get rid of the NaN occouring?].
                            PHP Code:
                            <html>
                            <
                            head>
                            <
                            title>Untitled Document</title>
                            <
                            meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                            <
                            SCRIPT TYPE="text/javascript">// for numbers only......
                            <!--
                            // copyright 1999 Idocs, Inc. [url]http://www.idocs.com[/url]
                            // Distribute this script freely but keep this notice in place
                            function numbersonly(myfieldedec)
                            {
                            var 
                            key;
                            var 
                            keychar;

                            if (
                            window.event)
                               
                            key window.event.keyCode;
                            else if (
                            e)
                               
                            key e.which;
                            else
                               return 
                            true;
                            keychar String.fromCharCode(key);

                            // control keys
                            if ((key==null) || (key==0) || (key==8) || 
                                (
                            key==9) || (key==13) || (key==27))
                               return 
                            true;

                            // numbers
                            else if ((("0123456789£.").indexOf(keychar) > -1))
                               return 
                            true;

                            // decimal point jump
                            else if (dec && (keychar == "."))
                               {
                               
                            myfield.form.elements[dec].focus();
                               return 
                            false;
                               }
                            else
                               return 
                            false;
                            }

                            //-->
                            </SCRIPT>
                            </
                            head>
                            <
                            body>
                            <
                            input type="text" size="5" maxlength="5" onKeyPress="return numbersonly(this, event)" onblur="this.value=Math.ceil(parseInt(this.value)/5)*5">
                            <
                            input type="text" size="5" maxlength="5" onKeyPress="return numbersonly(this, event)" onblur="this.value=Math.ceil(parseInt(this.value)/5)*5">
                            <
                            input type="text" size="5" maxlength="5" onKeyPress="return numbersonly(this, event)" onblur="this.value=Math.ceil(parseInt(this.value)/5)*5">
                            </
                            body>
                            </
                            html

                            Comment


                            • #15
                              Code:
                              function rounder(aObj)
                              {
                                if(aObj.value=="")return;
                                aObj.value=Math.ceil(parseInt(aObj.value)/5)*5;
                              }
                              //-->
                              
                              </SCRIPT>
                              
                              </head>
                              
                              <body>
                              
                              <input type="text" size="5" maxlength="5" onKeyPress="return numbersonly(this, event)" onblur="rounder(this)">

                              Comment

                              Working...
                              X