Jump to content

ensuring there is input in at least 1 of 2 fields


peppericious

Recommended Posts

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.

Link to comment
Share on other sites

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'];
                }
            }
      }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.