Forum Moderators: open

Message Too Old, No Replies

Form Validation, Text Fields

         

meganp

4:38 pm on Dec 3, 2003 (gmt 0)

10+ Year Member



Hi,

I have validated radio buttons and checkboxes, but how would I validate that at least one Qty box (in the type="text" boxes) has a value entered (this is a shortened version of the form, there are actually at least 10 qty's) since each name is different? I know there must be another way other than validating each one individually.


<form name="premier" method="post" action="../forms/proc_pgpkg3.php">
<table width="720" border="0">
<tr align="center">
<td colspan="3"><h3>Package Selection</h3>
<p>You can choose from several options for your monthly package.</p>
</td>
</tr>
<tr>
<td class="right">&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="right"><strong>ID No.:</strong></td>
<td>&nbsp;</td>
<td><input name="ID" type="text" id="ID" size="15">
</td>
</tr>
<tr>
<td class="right"><strong>Name:</strong></td>
<td>&nbsp;</td>
<td><input name="Name" type="text" id="Name" size="60"></td>
</tr>
<tr>
<td class="right"><strong>E-Mail Address</strong>:</td>
<td>&nbsp;</td>
<td><input name="Email" type="text" id="Email" size="60"></td>
</tr>
<tr>
<td class="right"><strong>Phone Number:</strong></td>
<td>&nbsp;</td>
<td><input name="Phone" type="text" id="Phone" size="60"></td>
</tr>
<tr>
<td class="right">&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="right"><input type="radio" name="Autoship" value="Package_Selection"></td>
<td>&nbsp;</td>
<td>Below is my selection for the Autoship Package</td>
</tr>
<tr>
<td class="right"><input type="radio" name="Autoship" value="Change"></td>
<td>&nbsp;</td>
<td>I am changing my previous Autoship Package selection</td>
</tr>
<tr>
<td class="right"><input type="radio" name="Autoship" value="Separate_Selections"></td>
<td>&nbsp;</td>
<td>I would like to purchase the following package(s) separately from my
Autoship</td>
</tr>
<tr>
<td height="20" colspan="3">&nbsp;</td>
</tr>
<tr>
<td width="110" height="55" class="right">
PG0401 <br>
<strong>$38.00</strong><br>
<input name="Qty_PG0401" type="text" id="Qty_PG0401" size="3">
Qty<br></td><td width="3">&nbsp;</td>
<td width="593"><p><strong>Candles</strong><br>
Descriptive text here. <em>Selection
may vary.</em></p>
</td>
</tr>
<tr>
<td class="right"> PG0407 <br>
<strong>$36.00</strong><br>
<input name="Qty_PG0407" type="text" id="Qty_PG0407" size="3">
Qty</td><td>&nbsp;</td>
<td><strong>Casual Gifts</strong><br>
Descriptive text here. <em>Selection
may vary.</em></td>
</tr>
<tr>
<td class="right">PG0410<br>
<strong>$34.00</strong><br>
<input name="Qty_PG0410" type="text" id="Qty_PG0410" size="3">
Qty</td><td>&nbsp;</td>
<td><strong>Gift Wrap</strong><br>
Descriptive text here. <em>Selection may vary.</em></td>
</tr>
<tr>
<td class="right">&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>

Thank you for any assistance.
mp

AWildman

5:29 pm on Dec 3, 2003 (gmt 0)

10+ Year Member



You could use javascript. Get a reference to the form. Then loop through the form's elements. Where type = text, do an if to see if any of those elements has a value. If so, break. If not, throw up a message to the user, or whatever other action it is that you plan to take.

meganp

6:35 pm on Dec 3, 2003 (gmt 0)

10+ Year Member



Thanks... I'm not too literate in Javascript and looping through forms, etc., but I managed in the meantime to accomplish what I wanted (although I did have to do each individually) with this:


// check that at least one quantity is entered

if (window.document.premier.Qty_PG0401.value < 1 &&
window.document.premier.Qty_PG0407.value < 1 &&
window.document.premier.Qty_PG0410.value < 1)
{
alert ("You did not enter a quantity.");
return false;
}
return true;
}

When I tried other ways (before posting), I ended up with an array and had a lot of trouble retrieving the values properly with php.

Thanks again,
mp