Non-Mandatory Phone Field

I’m working on a form that has 3 fields that do not have to be completed if the individual radio button is marked. That part is working fine. The problem is with the phone number for the form that has the household button marked. This field should not be mandatory. The html and php code are as such:

<!-- Household Member Information -->
<div class="row form-row">
	<h4><strong><i>Household Member Information</i></strong></h4>
</div>
	
<div class="row form-row mt-3 mb-3">		
		<h5 class="text-success"><strong>If last name and/or phone is different please enter correct information.</strong></h5>
</div>
<div class="row form-row">
<div class="col-xl-4 col-lg-4 col-md-12 col-sm-12 col-xs-12">
	<h4><strong>First Name</strong></h4>
	<input type="text" class="form-control" id="inputHouseholdFirstName" name="householdfirstname" placeholder="Household Member FirstName">
</div>
<div class="col-xl-4 col-lg-4 col-md-12 col-sm-12 col-xs-12">
	<h4><strong>Last Name</strong></h4>
	<input type="text" class="form-control" id="inputLastName_2" name="householdlastname" placeholder="Household Member Last Name">
</div>

<div class="col-xl-4 col-lg-4 col-md-12 col-sm-12 col-xs-12">
	<h4><strong>Phone</strong></h4>
	<input type="text" class="form-control" id="inputHouseholdPhone" name="householdphone" placeholder="Household Member Phone">
</div>
</div>
</div></div>

$householdfirstname = check_input($_POST['householdfirstname'], "Please enter Household Member's First Name");
$householdfirstname = ucwords($householdfirstname);
$householdlastname = check_input($_POST['householdlastname'], "");
$householdlastname = ucwords($householdlastname);
$householdphone = check_input($_POST['householdphone'], "");
/* If phone is not 10 digits show error message */
if (!preg_match("/^\D*(\d\D*){10}$/", $householdphone)) {
	show_error("Invalid Household phone number");
}

}
$householdphone = format_householdphone($householdphone);
/* Household Member Phone Function */
function format_householdphone($householdphone) {
$householdphone = preg_replace("/[^0-9]/", “”, $householdphone);
if (strlen($householdphone) == 10)
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", “($1) $2-$3”, $householdphone);
else
return $householdphone;
}

I thought removing “Pleases enter Household Member’s Phone Number” would do the trick, but it didn’t.
Could someone please tell me what I need to change and/or add?
Thank you.

/* If phone is not 10 digits show error message */ why don’t you remove this part?

Removing that would open up the possibilities of an incorrect phone number being submitted.

and no number is a correct number? Then add this to your condition.

https://www.php.net/manual/en/language.operators.logical.php

Sorry but that’s way over my head.

Sponsor our Newsletter | Privacy Policy | Terms of Service