Mazer14 Posted March 10, 2011 Share Posted March 10, 2011 Having trouble figuring this out. 1) How can I check whether the user's input for a field is an integer with a value greater than 0? Was thinking of using regular expressions using 1-9 but then 10 might result in an error and that's not wanted. 2) For a field that's a drop-down, how can I have an error show if they choose the default option (value of "none") Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/ Share on other sites More sharing options...
MadLittleMods Posted March 10, 2011 Share Posted March 10, 2011 Should work but post back with any problems... 1) if(preg_match('/([0-9]{1,})/', $thing_to_check)) { // GOOD } else { // ERROR } 2) if(preg_match('/none/', $thing_to_check)) { // ERROR } Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185293 Share on other sites More sharing options...
Mazer14 Posted March 10, 2011 Author Share Posted March 10, 2011 Ahh, figured out #2 as you entered your reply if($_POST['field'] == 'none') Was what I had done. I'll look at your post. Thanks man! Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185295 Share on other sites More sharing options...
Mazer14 Posted March 10, 2011 Author Share Posted March 10, 2011 Cool, so I got those worked out. One more question though. Trying to have an error occur when they leave a text field blank: if (empty($_POST['name']) || $_POST['name'] == " " || $_POST['name'] == " " || $_POST['name'] == " ") Is there a method to make an error occur any time a string of spaces are entered instead of having to do " " and " " and " " and so on? Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185362 Share on other sites More sharing options...
MadLittleMods Posted March 10, 2011 Share Posted March 10, 2011 try... if(preg_match('/(\s]{0,})/', $thing_to_check)) { // GIVE THEM AN ERROR } Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185365 Share on other sites More sharing options...
Pikachu2000 Posted March 10, 2011 Share Posted March 10, 2011 There's no need to use a pattern for number 1. if( is_numeric($_POST['value']) && intval($_POST['value']) > 0 ) { // value validates } [/code] Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185376 Share on other sites More sharing options...
cyberRobot Posted March 10, 2011 Share Posted March 10, 2011 There's no need to use a pattern for number 1. if( is_numeric($_POST['value']) && intval($_POST['value']) > 0 ) { // value validates } [/code] As long as you're ok with things like the following being considered a number: +01.45 2.45 Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185539 Share on other sites More sharing options...
cyberRobot Posted March 10, 2011 Share Posted March 10, 2011 Should work but post back with any problems... 1) if(preg_match('/([0-9]{1,})/', $thing_to_check)) { // GOOD } else { // ERROR } As is, the above regular expression will match anything that contains a number; including "a2". It should work however by changing it to: <?php ... if(preg_match('/^([0-9]{1,}$)/', $thing_to_check) && $thing_to_check > 0) { // GOOD } else { // ERROR } ... ?> Note that I also added the secondary check to make sure it doesn't match 0. Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185542 Share on other sites More sharing options...
cyberRobot Posted March 10, 2011 Share Posted March 10, 2011 You could simplify the expression with the following: <?php ... if(preg_match("/^\d+$/", $thing_to_check) && $thing_to_check > 0) { // GOOD } else { // ERROR } ... ?> Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185543 Share on other sites More sharing options...
cyberRobot Posted March 10, 2011 Share Posted March 10, 2011 Cool, so I got those worked out. One more question though. Trying to have an error occur when they leave a text field blank: if (empty($_POST['name']) || $_POST['name'] == " " || $_POST['name'] == " " || $_POST['name'] == " ") Is there a method to make an error occur any time a string of spaces are entered instead of having to do " " and " " and " " and so on? You could trim the value before testing it: <?php ... $_POST['name'] = trim($_POST['name']); ... ?> For more information about trim(), check out: http://php.net/manual/en/function.trim.php Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185544 Share on other sites More sharing options...
Pikachu2000 Posted March 10, 2011 Share Posted March 10, 2011 There's no need to use a pattern for number 1. if( is_numeric($_POST['value']) && intval($_POST['value']) > 0 ) { // value validates } As long as you're ok with things like the following being considered a number: +01.45 2.45 I didn't mean is_numeric(); I never even use that myself. I meant ctype_digit() . . . if( ctype_digit($_POST['value']) && intval($_POST['value']) > 0 ) { // value validates } Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185792 Share on other sites More sharing options...
cyberRobot Posted March 10, 2011 Share Posted March 10, 2011 I didn't mean is_numeric(); I never even use that myself. I meant ctype_digit() . . . if( ctype_digit($_POST['value']) && intval($_POST['value']) > 0 ) { // value validates } Nice, I've been wondering if there was an alternative to regular expressions. A lot of people suggest is_numeric(). ctype_digit() appears to be that answer. http://php.net/manual/en/function.ctype-digit.php Out of curiosity, why did you include intval()? The code seems to work fine without it. It doesn't throw any warnings either with error_reporting(-1); Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185804 Share on other sites More sharing options...
Pikachu2000 Posted March 10, 2011 Share Posted March 10, 2011 Just to make sure the integer value is greater than zero. It's one of those things I've been doing forever and can't seem to stop, but yes, it should work fine either way. Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185806 Share on other sites More sharing options...
cyberRobot Posted March 10, 2011 Share Posted March 10, 2011 Just to make sure the integer value is greater than zero. It's one of those things I've been doing forever and can't seem to stop, but yes, it should work fine either way. Thanks for the info! Quote Link to comment https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/#findComment-1185821 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.