How to compare values from two dropdowns

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HelpPlzz
    New Member
    • May 2007
    • 3

    How to compare values from two dropdowns

    I want to compare the min and max values between two dropdowns. This script sometimes gives correct result ...and sometimes not... Pls can u answer me whts goin wrong sometimes.
    Code:
    <html>
    <head>
     
    <script type='text/JavaScript'>
     
    function validateBox()
    { 
     
    try
    {	var frm = document.form_srch_sales_basic;
    	 formMinValue = document.form_srch_sales_basic.txtMinPrice.options[document.form_srch_sales_basic.txtMinPrice.selectedIndex].value;
     
    formMaxValue = document.form_srch_sales_basic.txtMaxPrice.options[document.form_srch_sales_basic.txtMaxPrice.selectedIndex].value;
     
    if(formMinValue > formMaxValue)
    			{	
    				alert(formMinValue);
    				alert(formMaxValue);
    				alert("Your Minimum value is Greater then Maximum Value.Please Re-enter Maximum Value!");
    				return false;
    			}			
    			 return true;
    }
    catch(e)
    {
    alert(e);
    return false;
    }
    }
     
    </script>
     
    </head>
     
    <body>
    <form name="form_srch_sales_basic" onsubmit="return validateBox();" method ="post" action="view_sales.php">
    					<tr>
    					 <td align="center" width="95">&nbsp;</td>
    					 <td align="left" height="25" >Price Range</td>
    					</tr>
     
    					 <select size="1" name="txtMinPrice">
    <option value="" selected>No Minimum</option>
    <option value="250000">250,000</option>
    <option value="500000">500,000</option>
    <option value="750000">750,000</option>
    <option value="1000000">1,000,000</option>
    <option value="1250000">1,250,000</option>
    <option value="1500000">from $1,500,000</option>
    <option value="2000000">from $2,000,000</option>
    <option value="3000000">3,000,000</option>
    <option value="5000000">5,000,000</option>
    </select>
    				 <tr>
    				 <td align="center" width="95" height="6"></td>
    				 <td align="left" valign="top" height="6"></td>
    				</tr>
    					 <select size="1" name="txtMaxPrice">
    <option value="" selected>No Maximum</option>
    <option value="500000">500,000</option>
    <option value="750000">750,000</option>
    <option value="1000000">1,000,000</option>
    <option value="1250000">1,250,000</option>
    <option value="1500000">1,500,000</option>
    <option value="2000000">2,000,000</option>
    <option value="3000000">3,000,000</option>
    <option value="5000000">5,000,000</option>
    <option value="8000000">8,000,000</option>
    </select>
    					 <input type="submit" value="Submit">
    </form>
    </body>
    </html>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Instead of comparing the string value, you need to compare the integer values using parseInt, otherwise "1000000" will be regarded as less than "750000".

    Comment

    • kunal pawar
      Contributor
      • Oct 2007
      • 297

      #3
      Modify ur code, use eval() function at time of comparison.
      like below

      [CODE=javascript]<script type='text/JavaScript'>

      function validateBox()
      {

      try
      {
      var frm = document.form_s rch_sales_basic ;
      formMinValue = document.form_s rch_sales_basic .txtMinPrice.op tions[document.form_s rch_sales_basic .txtMinPrice.se lectedIndex].value;

      formMaxValue = document.form_s rch_sales_basic .txtMaxPrice.op tions[document.form_s rch_sales_basic .txtMaxPrice.se lectedIndex].value;

      if(eval(formMin Value) > eval(formMaxVal ue))
      {

      alert("Your Minimum value is Greater then Maximum Value.Please Re-enter Maximum Value!");
      return false;
      }
      return true;
      }
      catch(e)
      {
      alert(e);
      return false;
      }
      }

      </script>[/CODE]
      Last edited by acoder; Jan 30 '08, 01:22 PM. Reason: Added code tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by kunal pawar
        Modify ur code, use eval() function at time of comparison.
        You should avoid eval wherever possible. parseInt would be fine here.

        PS. don't forget those code tags.

        Comment

        Working...