peppericious Posted March 7, 2012 Share Posted March 7, 2012 I have 2 fields, one for Mother's mobile, the other for Father's mobile. I want to ensure that at least one of the 2 fields contains a number. This is what I have: <?php if(!isset($_POST['guardian1_mobile']) && !isset($_POST['guardian2_mobile'])) { // no number entered in either field $errors[] = 'You must enter a mobile number in at least one of the Guardian1 or Guardian2 Mobile fields.'; } else { // if Mother mobile if(!is_numeric($_POST['guardian1_mobile']) || strlen(trim($_POST['guardian1_mobile'])) != 7) { // number must be numeric and exactly 7 digits $errors[] = '<strong>Guardian1 mobile</strong>: please enter a 7-digit number without spaces or dashes.'; } else { $_SESSION['guardian1_mobile'] = $_POST['guardian1_mobile']; } // if Father mobile if(!is_numeric($_POST['guardian2_mobile']) || strlen($_POST['guardian2_mobile']) != 7) { $errors[] = '<strong>Guardian2 mobile</strong>: please enter a 7-digit number without spaces or dashes.'; } else { $_SESSION['guardian2_mobile'] = $_POST['guardian2_mobile']; } } However, when I leave both fields blank, errors for the 'else' part of the condition are returned ('Please enter a 7-digit number...'). But it should never get as far as the 'else'.... Can anyone tell me what's wrong with my logic in the first line?... Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/258461-ensuring-there-is-input-in-at-least-1-of-2-fields/ Share on other sites More sharing options...
kicken Posted March 7, 2012 Share Posted March 7, 2012 A text field is always set, as defined by isset(), even if it's empty. You can use the empty function to check that it is set, and has a non-false value. Quote Link to comment https://forums.phpfreaks.com/topic/258461-ensuring-there-is-input-in-at-least-1-of-2-fields/#findComment-1324838 Share on other sites More sharing options...
peppericious Posted March 7, 2012 Author Share Posted March 7, 2012 A text field is always set, as defined by isset(), even if it's empty. You can use the empty function to check that it is set, and has a non-false value. Excellent, thanks. Changed it all to this and it works perfectly. <?php // ensure that at least one of Mother or Father mobile is entered if(empty($_POST['mother_mobile']) && empty($_POST['father_mobile'])) { $errors[] = 'You must enter a mobile number in at least one of the Mother or Father Mobile fields.'; } else { // if Mother mobile if(!empty($_POST['mother_mobile'])) { if(!is_numeric($_POST['mother_mobile']) || strlen($_POST['mother_mobile']) != 7) { $errors[] = '<strong>Mother mobile</strong>: please enter a 7-digit number without spaces or dashes.'; } else { $_SESSION['mother_mobile'] = $_POST['mother_mobile']; } } // if Father mobile if(!empty($_POST['father_mobile'])) { if(!is_numeric($_POST['father_mobile']) || strlen($_POST['father_mobile']) != 7) { $errors[] = '<strong>Father mobile</strong>: please enter a 7-digit number without spaces or dashes.'; } else { $_SESSION['father_mobile'] = $_POST['father_mobile']; } } } Quote Link to comment https://forums.phpfreaks.com/topic/258461-ensuring-there-is-input-in-at-least-1-of-2-fields/#findComment-1324841 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.