desithugg Posted July 25, 2006 Share Posted July 25, 2006 umm i want to submit a form but i wanna check if it only contains numbers.I only want numbers enterd in that field.Cna any1 help me with this dont know where to start. Link to comment https://forums.phpfreaks.com/topic/15607-form-validiation/ Share on other sites More sharing options...
leeming Posted July 25, 2006 Share Posted July 25, 2006 [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 https://forums.phpfreaks.com/topic/15607-form-validiation/#findComment-63492 Share on other sites More sharing options...
wildteen88 Posted July 25, 2006 Share Posted July 25, 2006 Use is numeric:[code]<?phpif(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 https://forums.phpfreaks.com/topic/15607-form-validiation/#findComment-63494 Share on other sites More sharing options...
PHPSpirit Posted July 25, 2006 Share Posted July 25, 2006 Use the preg_match();Read this:http://www.php.net/preg_match Link to comment https://forums.phpfreaks.com/topic/15607-form-validiation/#findComment-63496 Share on other sites More sharing options...
desithugg Posted July 25, 2006 Author Share Posted July 25, 2006 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 https://forums.phpfreaks.com/topic/15607-form-validiation/#findComment-63500 Share on other sites More sharing options...
wildteen88 Posted July 25, 2006 Share Posted July 25, 2006 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]<?phpif(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 https://forums.phpfreaks.com/topic/15607-form-validiation/#findComment-63502 Share on other sites More sharing options...
ryanlwh Posted July 25, 2006 Share Posted July 25, 2006 i'd check for magic quotes before adding slashes[code]$price = !get_magic_quotes_gpc() ? addslashes($_POST['price']) : $_POST['price'];[/code] Link to comment https://forums.phpfreaks.com/topic/15607-form-validiation/#findComment-63506 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.