Genesis5150 Posted July 4, 2015 Share Posted July 4, 2015 I want a php verification only if a value is submitted in a field and if so to make sure it has less than 3 characters. This what I have which checks if it has less than 3 characters but not sure how to only trigger if a value has been submitted.Thanks if (( strlen($_POST['office']) < 3 ) ){ $fielderror .="\"Office\" field cannot contain less than three characters<br>"; $doNotSubmit = true; } Quote Link to comment https://forums.phpfreaks.com/topic/297173-php-isset-question/ Share on other sites More sharing options...
Ch0cu3r Posted July 4, 2015 Share Posted July 4, 2015 As your title says use isset if (!isset($_POST['office'])) { $fielderror .= 'Please fill in the "Office" field'; $doNotSubmit = true; } else if(isset($_POST['office']) && strlen($_POST['office']) < 3) { $fielderror .= '"Office" field cannot contain less than three characters<br>'; $doNotSubmit = true; } Quote Link to comment https://forums.phpfreaks.com/topic/297173-php-isset-question/#findComment-1515573 Share on other sites More sharing options...
Genesis5150 Posted July 4, 2015 Author Share Posted July 4, 2015 Now that I'm thinking about it I'm trying to use !empty() to first check if it is empty and then do a check if there is a value to make sure the value is not less than 3 characters. This is what I have: if (empty($_POST['office'])) { $doNotSubmit = true; } // IF THE FORM FIELD IS TOO SHORT elseif (strlen($_POST['office']) < 3 ) { $fielderror .="\"Office\" field cannot contain less than three characters<br>"; $doNotSubmit = true; } But the code above didn't work.Thanks Quote Link to comment https://forums.phpfreaks.com/topic/297173-php-isset-question/#findComment-1515575 Share on other sites More sharing options...
Ch0cu3r Posted July 4, 2015 Share Posted July 4, 2015 What do you mean "didn't work" could you explain what you are expecting that code to do? Quote Link to comment https://forums.phpfreaks.com/topic/297173-php-isset-question/#findComment-1515579 Share on other sites More sharing options...
Genesis5150 Posted July 4, 2015 Author Share Posted July 4, 2015 Sorry for the misunderstanding. What I'm trying to look for is a code that only works if there a value to that field. If a value has been posted then make sure it is not less than 3 characters. No error messages are displayed if it is empty and only display an error message if the value of that field is less than 3 characters if posted. Quote Link to comment https://forums.phpfreaks.com/topic/297173-php-isset-question/#findComment-1515582 Share on other sites More sharing options...
mac_gyver Posted July 5, 2015 Share Posted July 5, 2015 (edited) all text, textarea, password, hidden, select/option, and usually submit, form fields will be set if the form has been submitted. you would use an isset() statement to detect if a form has been submitted, like in your first thread on this site. note: zeros are considered to be empty(), so, someone entering a '0' will pass the empty() test, if the string length is what you are actually looking for, either test the value against an empty string '' or test the strlen() of the value. for your stated logic test - empty is okay or if not empty, three or more characters is okay. or conversely/complementary, the error is if not empty and less than three characters - $len = strlen($_POST['office']); // i'm going to assume that you have trimmed all the submitted data, so that spaces, tabs, and new-lines have been removed before/after any actual value if($len != 0 && $len < 3){ // error condition } another way to state this error condition would be if the length is greater than zero and less than 3 - if($len > 0 && $len < 3){ here's a hint that will simplify your error logic. use an array $errors (to initialize it - $errors = array(); ) and just set elements in the array for each error message ($errors[] ='"Office" field cannot contain less than three characters';). to test if there are any errors, just test if the array is empty(). if the array is empty, there are no errors. if it is not empty, there are errors. to display the error messages, just loop over the array or you can simply implode() it. Edited July 5, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/297173-php-isset-question/#findComment-1515586 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.