Jump to content


Photo

form validiation


  • Please log in to reply
6 replies to this topic

#1 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 25 July 2006 - 04:54 PM

umm i want to submit a form but i wanna check if it only contains numbers.I only want numbers enterd in that field.Cna any1 help me with this dont know where to start.

#2 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 25 July 2006 - 04:59 PM

ereg("[0-9]", $data);

will return true if numbers are found, but i think its...

ereg("^[0-9]", $data)

for only numbers

*sorry if im not 100% on this, but i always got confused, and have a function to do this instead lol*
Im not perfect ;) lol

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 July 2006 - 05:00 PM

Use is numeric:
<?php

if(isset($_POST['submit']))
{
    if(is_numeric($_POST['numb']))
    {
        echo "Is numeric!";
    }
    else
    {
        echo "Is not numeric!";
    }
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <input type="text" name="numb" value="<?php echo @$_POST['numb']; ?>" /><br />
  <input type="submit" name="submit" value="Check number" />
</form>
Or you can use ereg

#4 PHPSpirit

PHPSpirit
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 25 July 2006 - 05:00 PM

Use the preg_match();

Read this:
http://www.php.net/preg_match


#5 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 25 July 2006 - 05:06 PM

umm would this work i think it shoudl
<?
$price = $_POST['price']; 
if ($price == "") 
{
echo"Sorry you didn't fill in the price field.";
exit();
}
if(is_numeric($price]))
{
$query = "INSERT INTO sold (price) VALUES ('$price')";  
$result = mysql_query($query);
}
   else
    {
        echo "Sorry you may only enter Numbers in the price field.Please go back and fix you're mistakes.";
}
?>


#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 July 2006 - 05:08 PM

Rather than do this:
<?
$price = $_POST['price']; 
if ($price == "") 
{
echo"Sorry you didn't fill in the price field.";
exit();
}
if(is_numeric($price]))
{
$query = "INSERT INTO sold (price) VALUES ('$price')";  
$result = mysql_query($query);
}
   else
    {
        echo "Sorry you may only enter Numbers in the price field.Please go back and fix you're mistakes.";
}
?>
I'd do this:
<?php

if(isset($_POST['price']) && !empty($_POST['price']) && is_numeric($_POST['price']))
{
    $price = addslashes($_POST['price']);

    $query = "INSERT INTO sold (price) VALUES ('$price')";
    $result = mysql_query($query);
}
else
{
        echo "PLease verify you have filled in the price field correctly";
}
?>


#7 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 25 July 2006 - 05:15 PM

i'd check for magic quotes before adding slashes
$price = !get_magic_quotes_gpc() ? addslashes($_POST['price']) : $_POST['price'];

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users