Jump to content

Archived

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

desithugg

form validiation

Recommended Posts

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.

Share this post


Link to post
Share on other sites
[code]
ereg("[0-9]", $data);
[/code]

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

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

for only numbers

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

Share this post


Link to post
Share on other sites
Use is numeric:
[code]<?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>[/code]
Or you can use ereg

Share this post


Link to post
Share on other sites
umm would this work i think it shoudl
[code]<?
$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.";
}
?>[/code]

Share this post


Link to post
Share on other sites
Rather than do this:
[code]<?
$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.";
}
?>[/code]
I'd do this:
[code]<?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";
}
?>[/code]

Share this post


Link to post
Share on other sites
i'd check for magic quotes before adding slashes
[code]$price = !get_magic_quotes_gpc() ? addslashes($_POST['price']) : $_POST['price'];[/code]

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.