Jump to content


Photo

Only Allow 0-9 in Input Box


  • Please log in to reply
4 replies to this topic

#1 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 11 October 2006 - 01:47 PM

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

<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>


#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 11 October 2006 - 01:55 PM

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:
<?php
$qty = $_POST['quantity'];
if (!preg_match('|^[0-9]$|', $qty)) {
  // contains wrong characters
  // throw error here
}
?>

hope that helps!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 11 October 2006 - 02:07 PM

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.

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 11 October 2006 - 02:28 PM

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:
<?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");
}
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 11 October 2006 - 02:34 PM

.. 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.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users