kevincro Posted November 7, 2007 Share Posted November 7, 2007 Can someone tell me what to add or remove to the following code in order to require a user to put something in the field? Or should I do this somewhere else? The following is a snippet of form validation code. function check_field2($Shipped) { if(!preg_match("/[^0-9\ ]+$/",$Shipped)) return TRUE; else return FALSE; } Link to comment https://forums.phpfreaks.com/topic/76448-form-validation/ Share on other sites More sharing options...
pocobueno1388 Posted November 7, 2007 Share Posted November 7, 2007 All you have to do is this <?php if (!isset($_POST['field_name']) || $_POST['field_name'] == ""){ die("This field is required!"); } ?> Link to comment https://forums.phpfreaks.com/topic/76448-form-validation/#findComment-387154 Share on other sites More sharing options...
PHP_PhREEEk Posted November 7, 2007 Share Posted November 7, 2007 We will need to know exactly what is acceptable input, or what exactly is undesired input. PhREEEk Link to comment https://forums.phpfreaks.com/topic/76448-form-validation/#findComment-387155 Share on other sites More sharing options...
kevincro Posted November 7, 2007 Author Share Posted November 7, 2007 It just has to be a number, and it can not be left blank. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/76448-form-validation/#findComment-387197 Share on other sites More sharing options...
PHP_PhREEEk Posted November 8, 2007 Share Posted November 8, 2007 It just has to be a number, and it can not be left blank. Thanks for the help. Well, not blank is easy enough (the code Poco provided should work), but when you say 'a number', what format are you looking for? Obviously '0' is a number, as is '000000000000000000000000000000000000'. Is there a minimum number of digits? A maximum? Are they allowed separators like '-'? Would you like to force a separator? Is there a specific format using a separator, like 3 digits, then '-', then 3 digits, another '-', then 4 digits? Or just exactly 7 digits? I think you are getting the point... you can be extremely specific or be rather flexible. The more flexible you are, the more chance someone can enter garbage (either by mistake or otherwise). PhREEEk Link to comment https://forums.phpfreaks.com/topic/76448-form-validation/#findComment-387281 Share on other sites More sharing options...
teng84 Posted November 8, 2007 Share Posted November 8, 2007 is_int() Link to comment https://forums.phpfreaks.com/topic/76448-form-validation/#findComment-387286 Share on other sites More sharing options...
Crew-Portal Posted November 8, 2007 Share Posted November 8, 2007 <?php // For making a validation code try this: function random_string($max = 20) { //create a unique confirmation code. $chars = explode(" ", "a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9"); for($i = 0; $i < $max; $i++){ $rnd = array_rand($chars); $rtn .= md5($chars[$rnd]); } return substr(str_shuffle(strtolower($rtn)), 0, $max); } // For making a thing that makes sure a username or whatever has correct cahractors and inbetween lengths try: function valid_username($username) { //validate username if(ereg("^[a-zA-Z0-9]+$",$username) && strlen($username) >= 3 && strlen($username) <= 12) return true; else return false; } ?> Link to comment https://forums.phpfreaks.com/topic/76448-form-validation/#findComment-387296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.