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>"; } Link to comment https://forums.phpfreaks.com/topic/97592-numeric-validation-of-textbox/ 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 Link to comment https://forums.phpfreaks.com/topic/97592-numeric-validation-of-textbox/#findComment-499317 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"; } Link to comment https://forums.phpfreaks.com/topic/97592-numeric-validation-of-textbox/#findComment-499322 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> Link to comment https://forums.phpfreaks.com/topic/97592-numeric-validation-of-textbox/#findComment-499323 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; ?> Link to comment https://forums.phpfreaks.com/topic/97592-numeric-validation-of-textbox/#findComment-499330 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> Link to comment https://forums.phpfreaks.com/topic/97592-numeric-validation-of-textbox/#findComment-499335 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; ?> Link to comment https://forums.phpfreaks.com/topic/97592-numeric-validation-of-textbox/#findComment-499353 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?? Link to comment https://forums.phpfreaks.com/topic/97592-numeric-validation-of-textbox/#findComment-499370 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)'; } } Link to comment https://forums.phpfreaks.com/topic/97592-numeric-validation-of-textbox/#findComment-499372 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.