www.webdeveloper.com
Results 1 to 7 of 7

Thread: Allowing Only Certain Numbers and/or Increments of Numbers In Form Field

  1. #1
    Join Date
    Apr 2009
    Posts
    346

    Allowing Only Certain Numbers and/or Increments of Numbers In Form Field

    I have a form field which only allows increments of 25 to be entered into the form field, but now I need it to also allow the number 0. Here is the existing code:

    <input type="text" size="3" onChange="this.value = Math.max(Math.round(parseInt(0 + this.value, 10) / 25) * 25, 50);" name="product_qty" value="50" />';

    Anyone know how to make this allow 0??

  2. #2
    Join Date
    Nov 2010
    Posts
    978
    what is it supposed to do if the number isn't an increment of 25?

    Code:
    <input type="text" size="3" onfocus="oldval=this.value" onChange="if(this.value%25!=0){this.value=oldval}" name="product_qty" value="50" />

  3. #3
    Join Date
    Apr 2009
    Posts
    346
    It rounds up or down to the nearest increment of 25. So if someone types in 60, it'll round down to 50. Unfortunately, if someone types in 0, it rounds up to 25 as it is set now.

  4. #4
    Join Date
    Nov 2010
    Posts
    978
    is this what you mean?
    Code:
    <input type="text" size="3" onchange="if(this.value%25!=0){this.value = 25 * Math.round(this.value/25)}" name="product_qty" value="50" />

  5. #5
    Join Date
    Apr 2009
    Posts
    346
    Quote Originally Posted by xelawho View Post
    is this what you mean?
    Code:
    <input type="text" size="3" onchange="if(this.value%25!=0){this.value = 25 * Math.round(this.value/25)}" name="product_qty" value="50" />
    That actually worked great! Thanks.

    I hate to push my luck but I have a similar code that allows an increment of 12 as long as it's 12 or 24, and then anything over 25 it rounds up or down to the nearest 25 increment like the other code did. Is there a way to make this allow a zero as well?

    HTML Code:
    <input type="text" size="4" onChange="var n = parseInt(0 + this.value, 10), x = n <= 50 ? 12 : 25; this.value = Math.max(Math.round(n / x) * x, 12);" name="product_qty" value="12" />

  6. #6
    Join Date
    Nov 2010
    Posts
    978
    the "purists" will say that you shouldn't be mixing the javascript with the html, but that's your choice:
    Code:
    <input type="text" size="3" onchange="this.value=Number(this.value)<25?12 * Math.round(this.value/12):25 * Math.round(this.value/25)" name="product_qty" value="12" />
    I realized the other one can be simplified, btw...

    Code:
    <input type="text" size="3" onchange="this.value = 25 * Math.round(this.value/25)" name="product_qty" value="50" />
    Last edited by xelawho; 10-14-2012 at 09:03 PM.

  7. #7
    Join Date
    Apr 2009
    Posts
    346
    Thanks a ton for all your help!!! I was at my wits end with this. Thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles