mikebyrne Posted March 24, 2008 Share Posted March 24, 2008 I have two textboxes where I want the user to only be allowed to pass numeric values to my table. The textboxs are "Stockamount" & "Price". At present I've only validated for leaving the fields blank. How do I code the numeric validation? My code if ($_POST['Stockamount']=="") { $valid=0; $style_name = "background-color:#FF5959"; $error_amount = "Please enter the ammount of stock recivied<br>"; } if ($_POST['Price']=="") { $valid=0; $style_name = "background-color:#FF5959"; $error_price = "Please enter the product price<br>"; } Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted March 24, 2008 Share Posted March 24, 2008 if(!is_int($_GET['Price'])) { # This is what happens when the input isnt an integer. } also - try maybe a bit of JavaScript validation for the users end to save them entering thigns and continually submitting Quote Link to comment Share on other sites More sharing options...
zenag Posted March 24, 2008 Share Posted March 24, 2008 tis is numeric validation in php if(is_numeric($_POST['Stockamount'])) { echo "entered numeric value"; } else { echo "non numeric"; } Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted March 24, 2008 Author Share Posted March 24, 2008 So in my case? if(!is_numeric($_POST['Price'])) { $valid=0; $style_name = "background-color:#FF5959"; $error_amount = "Please enter numeric values only<br>"; } In terms of the Javascript side of things how would I code that around the textbox as Im not too familar with it? The one bit i do have is around my clear button <input type="reset" value="" style="border:0;background:url(../Admin_files/btn_clear.gif) no-repeat;width:73px;height:23px;" onclick="return confirm('Are you sure you want to clear all data?')"> The Price textbox looks like this <input type="text" class="itemEditForm04" name="Price" value="<?php echo $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...
lordfrikk Posted March 24, 2008 Share Posted March 24, 2008 Or you can use simple regex: <?php if (preg_match('#^\d+$#', $_POST['price'])): print "Is Numeric."; else: print "Isn't Numeric."; endif; ?> Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted March 24, 2008 Author Share Posted March 24, 2008 At present the validation still isnt working 100% If I leave the textbox blank I get both error reports If I insert only Letters = "Please enter a numeric value" If I insert numbers = No errors (which is correct) if ($_POST['Price']=="") { $valid=0; $style_name = "background-color:#FF5959"; $error_price = "Please enter the product price<br>"; } if(!is_numeric($_POST['Price'])) { $valid=0; $style_name = "background-color:#FF5959"; $error_price1 = "Please enter numeric values only"; } <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; ?><?php echo $error_price1; ?></font></td> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 24, 2008 Share Posted March 24, 2008 No need to for any complex regex <?php if(isset($_POST['Price'])) { if(empty($_POST['Price'])) { $price_error = 'Please provide a price'; } elseif(!is_numeric($_POST['Price'])) { $price_error = 'Price is invalid. Numbers only (eg: 123.45)'; } } ?> <input type="text" class="itemEditForm04" name="Price" value="<?php $row['Price'];?>" /> <?php if($price_error): ?><span style="font-size: 8pt; color:#FF0000"><?php echo $error_price; ?></span><?php endif; ?> Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted March 24, 2008 Author Share Posted March 24, 2008 See the problem I have is that my validation starts with if($_POST["action"] == "Add"){ $valid=1; if ($_POST['Price']=="") { $valid=0; $style_name = "background-color:#FF5959"; $error_price = "Please enter the product price<br>"; } if(!is_numeric($_POST['Price'])) { $valid=0; $style_name = "background-color:#FF5959"; $error_price1 = "Please enter numeric values only"; } How can I combine the $valid with your example?? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 24, 2008 Share Posted March 24, 2008 Just add $valid=0; in the if/else statement: if(isset($_POST['Price'])) { $valid = 1; if(empty($_POST['Price'])) { $valid = 0; $price_error = 'Please provide a price'; } elseif(!is_numeric($_POST['Price'])) { $valid = 0; $price_error = 'Price is invalid. Numbers only (eg: 123.45)'; } } 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.