I'm having some issues getting my validation to work. I essentially need to make sure that all textbox's are populated and at least 1 checkbox is ticked. The text fields are easy enough, but the way the checkbox's are populated is making it hard. Here is a snippet what my form looks like, there are a lot more after this. I can change the event on the checkbox's or remove them?
<form id="orderForm" action="#" method="get" onsubmit="return cdSubmitValidate();">
<section id="selectCD">
<h2>Select CDs</h2>
<div class='item'>
<span class='CDTitle'>A Biography of the Rev. Absalom Dawe</span>
<span class='CDYear'>2001</span>
<span class='catDesc'>Comedy</span>
<span class='CDPrice'>11.70</span>
<span class='chosen'><input type='checkbox' name='CD[]' value='651' title='11.70' onclick='checkBoxCheck(this)'/></span>
</div>
<div class='item'>
<span class='CDTitle'>A Bronx Tale Soundtrack</span>
<span class='CDYear'>2000</span>
<span class='catDesc'>Death Metal</span>
<span class='CDPrice'>7.80</span>
<span class='chosen'><input type='checkbox' name='CD[]' value='652' title='7.80' onclick='checkBoxCheck(this)'/></span>
</div>
<div class='item'>
<span class='CDTitle'>A Little Deeper</span>
<span class='CDYear'>2000</span>
<span class='catDesc'>Rap/R&B</span>
<span class='CDPrice'>8.30</span>
<span class='chosen'><input type='checkbox' name='CD[]' value='653' title='8.30' onclick='checkBoxCheck(this)'/></span>
</div>
<div id="retCustDetails" class="custDetails">
Forename <input type="text" name="forename" id="forename" />
Surname <input type="text" name="surname" id="surname" />
</div>
<p><input type="submit" name="submit" value="Order now!" id="sub1"/></p>
Tried this so far to no avail.
function cdSubmitValidate(){
var forename = document.getElementById('forename').value;
var surname = document.getElementById('surname').value;
var checkboxTicked;
function checkBoxCheck(elemName){
if (elemName.checked == true) {
checkboxTicked = true;
}else if (elemName.checked == false) {
checkboxTicked = false;
}
};
if (!forename || !surname || checkboxTicked) {
alert("Input criteria not met. Please Review and try again");
return false;
}
};
I think this will fix it, first moved checkBoxCheck()
function out of cdSubmitValidate()
function, also the checkboxTicked
. I think the problem was callign a function inside a fuction the console logged:
Uncaught ReferenceError: checkBoxCheck is not defined.
Also in your if
statement the condition was
if (!forename || !surname || checkboxTicked)
I changed it into if (!forename || !surname || !checkboxTicked)
var checkboxTicked; function cdSubmitValidate() { var forename = document.getElementById('forename').value, surname = document.getElementById('surname').value; if (!forename || !surname || !checkboxTicked) { alert("Input criteria not met. Please Review and try again"); return false; } }; function checkBoxCheck(elemName) { if (elemName.checked == true) { checkboxTicked = true; } else if (elemName.checked == false) { checkboxTicked = false; } };
<form id="orderForm" action="#" method="get" onsubmit="return cdSubmitValidate();"> <section id="selectCD"> <h2>Select CDs</h2> <div class='item'> <span class='CDTitle'>A Biography of the Rev. Absalom Dawe</span> <span class='CDYear'>2001</span> <span class='catDesc'>Comedy</span> <span class='CDPrice'>11.70</span> <span class='chosen'><input type='checkbox' class="chkbx" name='CD[]' value='651' title='11.70' onclick='checkBoxCheck(this)'/></span> </div> <div class='item'> <span class='CDTitle'>A Bronx Tale Soundtrack</span> <span class='CDYear'>2000</span> <span class='catDesc'>Death Metal</span> <span class='CDPrice'>7.80</span> <span class='chosen'><input type='checkbox' class="chkbx" name='CD[]' value='652' title='7.80' onclick='checkBoxCheck(this)'/></span> </div> <div class='item'> <span class='CDTitle'>A Little Deeper</span> <span class='CDYear'>2000</span> <span class='catDesc'>Rap/R&B</span> <span class='CDPrice'>8.30</span> <span class='chosen'><input type='checkbox' class="chkbx" name='CD[]' value='653' title='8.30' onclick='checkBoxCheck(this)'/></span> </div> <div id="retCustDetails" class="custDetails"> Forename <input type="text" name="forename" id="forename" /> Surname <input type="text" name="surname" id="surname" /> </div> <p><input type="submit" name="submit" value="Order now!" id="sub1"/></p>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.