0
Within a <form>
I have two formularies but one is not mandatory, only if the person wants to enter a new address. the problem that this happens is that regardless of whether the person clicks on the checkbox, the javascript validation requires the user to fill in the checkbox data even if the checkbox is not clicked. Is there any way around this with javascript?
HTML code:
<form name="form" action="orders.php" method="POST" onsubmit="return Validatedatacheckout()" >
<table id="address" style="display:none">
<tr>
<td>
Door number:<input type="text" placeholder="doornumber" name="doornumber" value="" id="doornumber">
Road:<input type="text" placeholder="road" name="road" value="" id ="road">
Country:<input type="text" placeholder="country" name="country" value="" id="country">
Post Code:<input type="text" placeholder="postcode" name="postcode" value="" id="postcode"> </td>
</tr>
</table>
<label><input name ="differentAddress" id="differentAddress" type='checkbox'>Use a different home address</label>
<br>
<br>
<h5>Please enter your bank details.</h5>
<div class="form-container">
<span class="input-label">First Name:</span><input type="text" placeholder="First name" name="firstName"> <br>
<span class="input-label">Last Name:</span><input type="text" name="lastName" placeholder="Last Name"> <br>
<span class="input-label">Account Number:</span><input type="text" name="accountnumber" placeholder="account number" onkeypress="return justNumber(event)"> <br>
<span class="input-label">Security Number:</span><input type="text" name="securitynumber" placeholder="security number" onkeypress="return justNumber(event)">
</div>
<br>
<br>
<input id="checksub" type="submit" value="Submit">
</form>
JAVASCRIPT:
function Validatedatacheckout(){
var doornumber = document.form.doornumber;
var road= document.form.road;
var postcode = document.form.postcode;
var country = document.form.country;
var accountnumber = document.form.accountnumber;
var securitynumber = document.form.securitynumber;
if (doornumber.value == "")
{
window.alert("Please enter a door number.");
doornumber.focus();
return false;
}
if (road.value == "")
{
window.alert("Please enter a road name.");
road.focus();
return false;
}
if (postcode.value == "")
{
window.alert("Please enter a post code.");
postcode.focus();
return false;
}
if (country.value == "")
{
window.alert("Please enter a country.");
country.focus();
return false;
}
if (accountnumber.value == "")
{
window.alert("Please enter an account number.");
accountnumber.focus();
return false;
}
if (securitynumber.value == "")
{
window.alert("Please enter a security number.");
securitynumber.focus();
return false;
}
}
In this case, the first part of the form will not be mandatory, only if the user selects the option
differentAddress
? If yes, just check the values of the fields only if a value ofcheckbox
is true (make aif
).– Woss