Jump to content

form validiation


desithugg

Recommended Posts

[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*
Link to comment
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
Link to comment
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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.