SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Australia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quantity has to be multiple of a number

    Hi there,

    Just wondering if anyone can help me out. Say I have quantity box stating minimum value (say 2). I want it can only accept multiple of 2 (ie 2,4,6,8, etc). What are the formula can I use to create alert for wrong input (ie 3,5,7 etc). The minimum value will vary, it can be any number and the multiple has to be of that number.

    Thank you.

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the modulus operator (%) to do this:

    Code:
    var minimum_value = 2;
    var the_value = path.to.the.box; // pseudo code
    
    if( the_value % minimum_value != 0 ) {
      alert('the entered value is not valid');
    }
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Evangelist nsj's Avatar
    Join Date
    Oct 2005
    Location
    Jamaica (W.I)
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if(multiple/2 == 0){
         // multiple is an even number
    }else{
        // multiple is odd
    }

  4. #4
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nsj: That won't work, since what it does is to check whether or not the result (not the remainder) from the division is zero.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Australia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lilleman
    You can use the modulus operator (%) to do this:

    Code:
    var minimum_value = 2;
    var the_value = path.to.the.box; // pseudo code
    
    if( the_value % minimum_value != 0 ) {
      alert('the entered value is not valid');
    }
    That is exactly what I need. How do I set it so it alerts onchange (customer input). Minimum value will be the value of quantity textbox.

    Thank you very much

  6. #6
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it something like this that you're looking for?

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Use of the Modulus Operator</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <script type="text/javascript">
          window.onload = function()
          {
            document.forms['foo'].elements['input'].onchange = function()
            {
              var minimum_value = this.form.elements['quantity'].value;
              if( this.value % minimum_value != 0 ) alert('not valid');
            }
          }
        </script>
      </head>
      
      <body>
        <form id="foo" action="your-script.php">
          <div>quantity: <input type="text" name="quantity" value="2" /></div>
          <div>customer input: <input type="text" name="input" /></div>
        </form>
      </body>
    </html>
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Australia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi lilleman,

    Thank you very much for your help. I don't think window.onload will work well because I want to use the function for multiple forms. I came up with a code that works but I can't figure out how to make the function usable for multiple forms. I've used form name as a hidden field. I'm not familiar with javascript and not sure how to assign form name to the function.

    Sample Code (only work for first Qty box)
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <script language="JavaScript">
    function checkQty ()
    {
    	var minimum_value = document.getElementById("MinQuantity").value ;
    	var the_value = document.getElementById("Quantity").value; 
    if( the_value % minimum_value != 0 ) {
      alert('the entered value is not valid')  ;  
      document.getElementById("Quantity").value = minimum_value;
    }
    }</script>
    
     <form id="form1" name="form1" method="post" action="">
      Qty  <input name="Quantity" type="text" onchange="checkQty()" value="6" />
      <input type="hidden" name="MinQuantity" value="6" />
        <input type="hidden" name="FormName" value="form1" />
    </form>
    
     <form id="anotherform" name="anotherform" method="post" action="">
      Qty  <input name="Quantity" type="text" onchange="checkQty()" value="8" />
      <input type="hidden" name="MinQuantity" value="8" />
      <input type="hidden" name="FormName" value="anotherform" />
    </form>
    
     <form id="myform" name="myform" method="post" action="">
      Qty  <input name="Quantity" type="text" onchange="checkQty()" value="12" />
      <input type="hidden" name="MinQuantity" value="12" />
      <input type="hidden" name="FormName" value="myform" />
    </form>
    </body>
    </html>

  8. #8
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's some code that seems to work properly:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Support for Multiple Forms</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <script type="text/javascript">
          window.onload = function()
          {
            for( var i = 0; i < document.forms.length; i++ )
            {
              var currentForm = document.forms[i];
              
              currentForm.elements['Quantity'].onchange = function()
              {
                var minimum_value = this.form.elements['MinQuantity'].value;
                
                if( this.value % minimum_value != 0 )
                {
                  this.value = minimum_value;
                  
                  alert('the given value is not valid');
                }
              }
            }
          }
        </script>
      </head>
      
      <body>
        <form method="post" action="">
          Qty  <input name="Quantity" type="text" value="6" />
          <input type="hidden" name="MinQuantity" value="6" />
          <input type="hidden" name="FormName" value="form1" />
        </form>
        <form method="post" action="">
          Qty  <input name="Quantity" type="text" value="8" />
          <input type="hidden" name="MinQuantity" value="8" />
          <input type="hidden" name="FormName" value="form1" />
        </form>
        <form method="post" action="">
          Qty  <input name="Quantity" type="text" value="12" />
          <input type="hidden" name="MinQuantity" value="12" />
          <input type="hidden" name="FormName" value="form1" />
        </form>
      </body>
    </html>
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Australia
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is it safe toi put it after </head> tag?

    Hi Lilleman...you're the man hehehe...

    That works nicely. One thing that I had to do though because of server scription restriction. I can't put the script in the head section. I have tested putting it in the body section and it still works fine. I hope there won't be a glitch from this. What are the disadvantages by doing it this way?


Bookmarks

Posting Permissions

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