Jump to content

Archived

This topic is now archived and is closed to further replies.

156418

Only Allow 0-9 in Input Box

Recommended Posts

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">&pound;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">&pound;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">&pound;3
      <input type="hidden" name="PPrice" value="3" size="10" maxlength="1"></p>
</td>
  </tr>
</table>
[/code]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]
<?php
if (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

Share this post


Link to post
Share on other sites
.. 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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.