mikebyrne Posted March 24, 2008 Share Posted March 24, 2008 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> Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 24, 2008 Share Posted March 24, 2008 you can use intval to force a string to an integer value. you can also you preg_match as a MUCH more effective way of validating strings. php.net/preg_match php.net/intval hope this helps, Quote Link to comment Share on other sites More sharing options...
soycharliente Posted March 24, 2008 Share Posted March 24, 2008 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. Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted March 24, 2008 Author Share Posted March 24, 2008 It just euros and cents. Id rather have it as it is but I cant get the validation to work and its driving me mental Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 24, 2008 Share Posted March 24, 2008 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 hope this helps, Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted March 24, 2008 Author Share Posted March 24, 2008 How can I apply this to my "Price" validation really not sure how to code your example with mine Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 24, 2008 Share Posted March 24, 2008 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, Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted March 24, 2008 Author Share Posted March 24, 2008 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] Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 25, 2008 Share Posted March 25, 2008 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 Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted March 25, 2008 Author Share Posted March 25, 2008 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')"; Quote Link to comment Share on other sites More sharing options...
uniflare Posted March 25, 2008 Share Posted March 25, 2008 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 Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted March 25, 2008 Author Share Posted March 25, 2008 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 Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted March 25, 2008 Author Share Posted March 25, 2008 Fixed the problem!! $price_error should have been $error_price!!!!!! Quote Link to comment Share on other sites More sharing options...
lordfrikk Posted March 25, 2008 Share Posted March 25, 2008 ^ lol Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted March 25, 2008 Author Share Posted March 25, 2008 ^ lol Days pulling my hair out!! Dont you just love coding sometimes!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.