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; } Quote Link to comment 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!"); } ?> Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 8, 2007 Share Posted November 8, 2007 is_int() Quote Link to comment 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; } ?> 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.