156418 Posted October 11, 2006 Share Posted October 11, 2006 I have a basic table/form which is passing the various inputs onto the next stage, what I want to do is where the Quantity box is only allow the use to enter a number, and reject on blank or anything else.Could someone show an easy way to do that, all the validation scripts I've found seem rather complicated or prevent the input being sent in the headers.Many thanks[code]<table width="70%" border="0"> <tr> <td width="13%" bgcolor="#E6D0DD">Quantity</td> <td width="74%" bgcolor="#E6D0DD">Ticket Type </td> <td width="13%" bgcolor="#E6D0DD">Price</td> </tr> <tr> <td bgcolor="#E6D0D2"><div align="center"> <input name="AdultQuantity" type="text" value="0" size="5"> </div></td> <td bgcolor="#E6D0D2">Adult <input type="hidden" name="ATicketType" value="Adult" size="13" maxlength="20"></td> <td bgcolor="#E6D0D2">£9 <input type="hidden" name="APrice" value="9" size="10" maxlength="1"></td> </tr> <tr> <td bgcolor="#E6D0D2"><div align="center"> <input name="ChildQuantity" type="text" value="0" size="5"> </div></td> <td bgcolor="#E6D0D2">Child <input type="hidden" name="CTicketType" value="Child" size="13" maxlength="20"></td> <td bgcolor="#E6D0D2">£6 <input type="hidden" name="CPrice" value="6" size="10" maxlength="1"></td> </tr> <tr> <td bgcolor="#E6D0D2"><div align="center"> <input name="PresentQuantity" type="text" value="0" size="5"> </div></td> <td bgcolor="#E6D0D2">Present <input type="hidden" name="PTicketType" value="Present" size="13" maxlength="20"></td> <td bgcolor="#E6D0D2">£3 <input type="hidden" name="PPrice" value="3" size="10" maxlength="1"></p></td> </tr></table>[/code] Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 11, 2006 Share Posted October 11, 2006 first off, just make sure that your quantity input has a maxlength of "1"... then, since this is in the PHP forum, i'm assuming you want to let PHP handle the validation. you would simply take your value that is passed in and run a pattern match on it:[code]<?php$qty = $_POST['quantity'];if (!preg_match('|^[0-9]$|', $qty)) { // contains wrong characters // throw error here}?>[/code]hope that helps! Quote Link to comment Share on other sites More sharing options...
156418 Posted October 11, 2006 Author Share Posted October 11, 2006 Thanks, I can see the logic that it would implement, but (forgive the ignorance!) where do I place that code, as whereever it goes on my page, or next page it ignores it - I have changed the name to match that of the input with no sucess.I should also have said that if they enter anything other than a number in the box it should redirect them to an error page. Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 11, 2006 Share Posted October 11, 2006 you have to place that code within your form handling. the code you posted above was only your form. wherever you are handling your form, you need to check all your variables. if you are wanting to check the data entered before the form is submitted, you can do it with javascript instead. without knowing what your form handling code is, though, we really can't help much. here's an example of how a handling page may look:[code]<?phpif (isset($_POST['submit'])) { // form has been submitted if (!preg_match('|^[0-9]$|', $qty)) { // send them to your error page header("Location: error.php"); }} else { // send them back to the form header("Location: myForm.php");}?>[/code]hope this helps Quote Link to comment Share on other sites More sharing options...
AndyB Posted October 11, 2006 Share Posted October 11, 2006 .. or make it bullet-proof by using an HTML select (drop-down box) as the method of entering a number with zero being the default. 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.