Jump to content

Numeric validation failing


mikebyrne

Recommended Posts

Im trying to code my text box for "PRICE" so the user cannot leave it blank and can only enter numeric values. At present the validation works if I leave it blank but doesnt validate for numeric values at present

 

My validation

 

<?PHP

require_once("adminconnect.php");

$ProductNo = mysql_real_escape_string(trim($_POST['ProductNo']));
$ProductName = mysql_real_escape_string(trim($_POST['ProductName']));
$Description = mysql_real_escape_string(trim($_POST['Description']));
$Price = mysql_real_escape_string(trim($_POST['Price']));
$Stockamount = mysql_real_escape_string(trim($_POST['Stockamount']));
$Type = $_POST['Type'];
$Display = strtoupper($_POST['display']); 

$tbl_name="product";

if($_POST["action"] == "Add"){

        $valid=1;
	if ($_POST['Type'] =="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_type = "Please enter the product type<br>";
							 }	

	if ($_POST['ProductName']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_name = "The Product Name seems to be mising?<br>";
                                		}

	if ($_POST['Description']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_description = "Your description seems to be mising?<br>";
									}
	if ($_POST['Stockamount']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_amount = "Please enter the ammount of stock recivied<br>";
									}
if(isset($_POST['Price']))
{
    $valid = 1;							
	if(empty($_POST['Price']))
    {
	$valid=0;
                $style_name = "background-color:#FF5959";
                $error_price = "Please enter the price<br>";
    }
    elseif(!is_numeric($_POST['Price']))
    {
        $valid = 0;
$style_name = "background-color:#FF5959";
        $price_error = 'Price is invalid. Numbers only (eg: 123.45)';
    }
}									
if ($valid==1) {       
                $sql="INSERT INTO $tbl_name (Producttype, ProductName, Description,Price, Stockamount, Display) VALUES ('$Type','$ProductName', '$Description', '$Price','$Stockamount''$Display')";
?>

<input type="text" class="itemEditForm04" name="Price" value="<?php $row['Price'];?>" /> <td width="269"><font color="#FF0000" style="font-size: 8pt"><?php echo $error_price; ?></font></td>

Link to comment
Share on other sites

What type of number are they inputing? I ran into that problem once and I just moved to a drop down menu for the dollars and another one for the cents. The only thing I had to check for them was to make sure it wasn't empty as the default value in each select menu had a value of an empty string.

Link to comment
Share on other sites

just use preg_match:

 

<?php

$numbers[] = "12.1"; // no match
$numbers[] = ".12"; // no match
$numbers[] = ".1"; // no match
$numbers[] = "12."; // no match
$numbers[] = "1,123.1"; // no match
$numbers[] = "12"; // match
$numbers[] = "12.12"; // match
$numbers[] = "0.12"; // match
$numbers[] = "123.12"; // match
$numbers[] = "1,123.12"; // match

$i=0;
foreach($numbers As $number){
   $number = str_replace(",","",$number);
   if(preg_match("/\A[0-9]{1,16}(\.[0-9]{2}){0,1}$/",$number) == 1){
      echo($numbers[$i]." Is Valid <br>");
   }else{
      echo($numbers[$i]." Is NOT Valid <br>");
   }
   $i++;
}

?>

 

this snippet uses preg_match to test each item in the $numbers array and echos accordingly. im using this to show an example of how to use preg_match and which numbers this particular REGEX will allow :D

 

hope this helps,

Link to comment
Share on other sites

    elseif(!is_numeric($_POST['Price']))
    {
        $valid = 0;
$style_name = "background-color:#FF5959";
        $price_error = 'Price is invalid. Numbers only (eg: 123.45)';
    }

 

becomes:

 

    elseif(preg_match("/\A[0-9]{1,16}(\.[0-9]{2}){0,1}$/",$number) != 1)
    {
        $valid = 0;
$style_name = "background-color:#FF5959";
        $price_error = 'Price is invalid. Numbers only (eg: 123.05)';
    }

 

hope this helps,

Link to comment
Share on other sites

This still doesnt produce and error if I type in letters instead of numeric values. Im pretty sure it to do with

 

if(isset($_POST['Price']))

{

    $valid = 1;

 

 

My complete code

 

<?PHP

require_once("adminconnect.php");

$ProductNo = mysql_real_escape_string(trim($_POST['ProductNo']));
$ProductName = mysql_real_escape_string(trim($_POST['ProductName']));
$Description = mysql_real_escape_string(trim($_POST['Description']));
$Price = mysql_real_escape_string(trim($_POST['Price']));
$Stockamount = mysql_real_escape_string(trim($_POST['Stockamount']));
$Type = $_POST['Type'];
$Display = strtoupper($_POST['display']); 

$tbl_name="product";

if($_POST["action"] == "Add"){

        $valid=1;
	if ($_POST['Type'] =="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_type = "Please enter the product type<br>";
							 }	

	if ($_POST['ProductName']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_name = "The Product Name seems to be mising?<br>";
                                		}

	if ($_POST['Description']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_description = "Your description seems to be mising?<br>";
									}
	if ($_POST['Stockamount']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_amount = "Please enter the ammount of stock recivied<br>";
									}
if(isset($_POST['Price']))
{
    $valid = 1;							
	if(empty($_POST['Price']))
    {
	$valid=0;
                $style_name = "background-color:#FF5959";
                $error_price = "Please enter the price<br>";
    }
   elseif(preg_match("/\A[0-9]{1,16}(\.[0-9]{2}){0,1}$/",$number) != 1)
    {
        $valid = 0;
$style_name = "background-color:#FF5959";
        $price_error = 'Price is invalid. Numbers only (eg: 123.05)';
    }
}									
if ($valid==1) {       
                $sql="INSERT INTO $tbl_name (Producttype, ProductName, Description,Price, Stockamount, Display) VALUES ('$Type','$ProductName', '$Description', '$Price','$Stockamount''$Display')";
[\code]

Link to comment
Share on other sites

if(isset($_POST['Price']))
{							
if(empty($_POST['Price']))
{
	$valid=0;
	$style_name = "background-color:#FF5959";
	$error_price = "Please enter the price<br>";
}
elseif(preg_match("/\A[0-9]{1,16}(\.[0-9]{2}){0,1}$/",$_POST['Price']) != 1)
{
	$valid = 0;
	$style_name = "background-color:#FF5959";
	$price_error = 'Price is invalid. Numbers only (eg: 123.05)';
}else{
	$valid = 1;
}
}else{
$valid = 0;
$style_name = "background-color:#FF5959";
$error_price = "Please enter the price (Price Field Missing Error)<br>";
}

 

this is what you want i bellieve

Link to comment
Share on other sites

The pregmatch seems to be failing as I dont get an error if I put in letters for the price but i get an error if the Price textbox is left blank

 

<?PHP

require_once("adminconnect.php");

$ProductNo = mysql_real_escape_string(trim($_POST['ProductNo']));
$ProductName = mysql_real_escape_string(trim($_POST['ProductName']));
$Description = mysql_real_escape_string(trim($_POST['Description']));
$Price = mysql_real_escape_string(trim($_POST['Price']));
$Stockamount = mysql_real_escape_string(trim($_POST['Stockamount']));
$Type = $_POST['Type'];
$Display = strtoupper($_POST['display']); 

$tbl_name="product";

if($_POST["action"] == "Add"){

        $valid=1;
	if ($_POST['Type'] =="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_type = "Please enter the product type<br>";
							 }	

	if ($_POST['ProductName']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_name = "The Product Name seems to be mising?<br>";
                                		}

	if ($_POST['Description']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_description = "Your description seems to be mising?<br>";
									}
	if ($_POST['Stockamount']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_amount = "Please enter the ammount of stock recivied<br>";
									}

	if(empty($_POST['Price']))
									{
	$valid=0;
	$style_name = "background-color:#FF5959";
	$error_price = "Please enter the price<br>";
									}
    if(preg_match("/\A[0-9]{1,16}(\.[0-9]{2}){0,1}$/",$_POST['Price']) != 1)
									{
	$valid = 0;
	$style_name = "background-color:#FF5959";
	$price_error = 'Price is invalid. Numbers only (eg: 123.05)';
									}							
if ($valid==1) {       
                $sql="INSERT INTO $tbl_name (Producttype, ProductName, Description,Price, Stockamount, Display) VALUES ('$Type','$ProductName', '$Description', '$Price','$Stockamount''$Display')";

 

Link to comment
Share on other sites

i see no reason why this should be happening...

 

save this code to a file called test.php and run it on your webserver:

 

<?php

$numbers[] = "12.1"; // no match
$numbers[] = ".12"; // no match
$numbers[] = ".1"; // no match
$numbers[] = "12."; // no match
$numbers[] = "1,123.1"; // no match

$numbers[] = "a1,123.1"; // no match
$numbers[] = "1,f123.1"; // no match
$numbers[] = "1,123.s1"; // no match
$numbers[] = "1,123.1f"; // no match
$numbers[] = "fadg.1"; // no match
$numbers[] = "1,123.sd"; // no match
$numbers[] = "gds.f"; // no match
$numbers[] = "sdgsdg"; // no match

$numbers[] = "12"; // match
$numbers[] = "12.12"; // match
$numbers[] = "0.12"; // match
$numbers[] = "123.12"; // match
$numbers[] = "1,123.12"; // match

$i=0;
foreach($numbers As $number){
   $number = str_replace(",","",$number);
   if(preg_match("/\A[0-9]{1,16}(\.[0-9]{2}){0,1}$/",$number) == 1){
      echo($numbers[$i]." Is Valid <br>");
   }else{
      echo($numbers[$i]." Is NOT Valid <br>");
   }
   $i++;
}

?>

 

give us the output

Link to comment
Share on other sites

The output is

 

12.1 Is NOT Valid

.12 Is NOT Valid

.1 Is NOT Valid

12. Is NOT Valid

1,123.1 Is NOT Valid

a1,123.1 Is NOT Valid

1,f123.1 Is NOT Valid

1,123.s1 Is NOT Valid

1,123.1f Is NOT Valid

fadg.1 Is NOT Valid

1,123.sd Is NOT Valid

gds.f Is NOT Valid

sdgsdg Is NOT Valid

12 Is Valid

12.12 Is Valid

0.12 Is Valid

123.12 Is Valid

1,123.12 Is Valid

 

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.