pixelgirl Posted April 14, 2008 Share Posted April 14, 2008 Hello, I am totally puzzled about this, as it works for some of my script, but for one validation section in js my code returns false even when it should be true. Its the if x > y price, age and height sections of the code below that are the problem, as they return false even when they should be true, and sometimes true when they should be false. Ive found the age and height are more of an issue than price, but all seem to be behaving a bit oddly. If the js misses them or gets them wrong the php validation picks them up, but the js should work first, and the php only if js not enabled. The js and form code is below. I cant see where the problem is, and i dont know if im just being dumb or its buggy for some other reason. see code below, any help would be appreciated (dotted lines show where problem code is) function checkForm() { var cHeightH = document.getElementById('HeightH'); var cHeightH2 = document.getElementById('HeightH2'); var cPrice = document.getElementById('txtPrice'); var cPrice2 = document.getElementById('txtPrice2'); var cAgeY = document.getElementById('AgeY'); var cAgeY2 = document.getElementById('AgeY2'); var cSexMare = document.getElementById('mare'); var cSexGelding = document.getElementById('gelding'); var cSexStallion = document.getElementById('stallion'); var cSexFilly = document.getElementById('filly'); var cSexColt = document.getElementById('colt'); var cShare = document.getElementById('share'); var cSale = document.getElementById('sale'); var cLoan = document.getElementById('loan'); if(cHeightH.value.length == 0 && cHeightH2.value.length != 0) { alert('Please select a minimum height in hands'); cHeightH.focus(); return false; } else if(cHeightH2.value.length == 0 && cHeightH.value.length != 0) { alert('Please select a maximum height in hands'); cHeightH2.focus(); return false; } --------- else if(cHeightH2.value.length != 0 && cHeightH.value.length != 0 && cHeightH.value > cHeightH2.value) { alert('Please ensure your minimum height selection is smaller than your maximum'); cHeightH.focus(); return false; } else if(cPrice.value.length == 0 && cPrice2.value.length != 0) { alert('Please select a minimum price'); cPrice.focus(); return false; } else if(cPrice2.value.length == 0 && cPrice.value.length != 0) { alert('Please select a maximum price'); cPrice2.focus(); return false; } ------------- else if(cPrice2.value.length != 0 && cPrice.value.length != 0 && cPrice.value > cPrice2.value) { alert('Please ensure your minimum price selection is smaller than your maximum'); cPrice.focus(); return false; } else if(cAgeY.value.length == 0 && cAgeY2.value.length != 0) { alert('Please select a minimum age in years'); cAgeY.focus(); return false; } else if(cAgeY2.value.length == 0 && cAgeY.value.length != 0) { alert('Please select a maximum age in years'); cAgeY2.focus(); return false; } -------------- else if(cAgeY2.value.length != 0 && cAgeY.value.length != 0 && cAgeY.value > cAgeY2.value) { alert('Please ensure your minimum age selection is smaller than your maximum'); cAgeY.focus(); return false; } else if (!cSexMare.checked && !cSexGelding.checked && !cSexStallion.checked && !cSexFilly.checked && !cSexColt.checked) { alert ('Please choose what sexes you are looking for'); return false; } else if (!cSale.checked && !cShare.checked && !cLoan.checked) { alert ('Please choose whether you are looking for horses for sale, share or loan'); return false; } else { return true; } } form code: <form method="post" action="searchresult.php" name="frmMatch" id="frmMatch"> <table width="100" align="left" cellspacing = "7"> <tr> <td> <table width="100" align="left" cellspacing = "7"> <tr> <td width="100"><p class="text3"><b>Height Between:</b></p></td> </tr> <tr> <td width="10"><select name="HeightH" id="HeightH"> <option value=""></option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> </select> </td> <td width="10"><p class="text3">Hands,</p></td> <td width="10"><select name="HeightI" id="HeightI"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> </td> <td width="10"><p class="text3">Inches (min)</p></td> </tr> <tr> <td width="100"><p class="text3"><b>And</b></p></td> </tr> <tr> <td width="10"><select name="HeightH2" id="HeightH2"> <option value=""></option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> </select> </td> <td width="10"><p class="text3">Hands,</p></td> <td width="10"><select name="HeightI2" id="HeightI2"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> </td> <td width="10"><p class="text3">Inches (max)</p></td> </tr> <tr> <td> </td> </tr> <tr> <td width="100"><p class="text3"><b>Price between:</b></p></td> </tr> <tr> <td width="10"><p class="text3"> £</p></td> <td colspan = "2" width="10"><input name="txtPrice" type="text" id="txtPrice" size="10"></td> <td width="10" colspan = "2"><p class="text3">(min)</p></td> </tr> <tr> <td width="100"><p class="text3"><b>And:</b></p></td> </tr> <tr> <td width="10"><p class="text3"> £</p></td> <td colspan = "2" width="10"><input name="txtPrice2" type="text" id="txtPrice2" size="10"></td> <td width="1o"><p class="text3">(max)</p></td> </tr> <tr> <td> </td> </tr> <tr> <td width="100"><p class="text3"><b>Age between:</b></p></td> </tr> <tr> <td><select name="AgeY" id="AgeY"> <option value=""></option> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> </select> </td> <td width="150"><p class="text3">Years,</p></td> <td><select name="AgeM" id="AgeM"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> </select> </td> <td width="150"><p class="text3">Months (min)</p></td> </tr> <tr> <td width="100"><p class="text3"><b>And:</b></p></td> </tr> <tr> <td><select name="AgeY2" id="AgeY2"> <option value=""></option> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> </select> </td> <td width="150"><p class="text3">Years,</p></td> <td><select name="AgeM2" id="AgeM2"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> </select> </td> <td width="150"><p class="text3">Months (max)</p></td> </tr> Quote Link to comment Share on other sites More sharing options...
friedemann_bach Posted April 14, 2008 Share Posted April 14, 2008 I think you should try to convert the field values to NUMERIC (use parseInt()), to make sure you are comparing numbers and not strings. Quote Link to comment Share on other sites More sharing options...
pixelgirl Posted April 14, 2008 Author Share Posted April 14, 2008 ok ill give that a go and see if its more stable. thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.