View Full Version : popup alert if no check boxes are selected
CraigR
06-17-2011, 08:52 AM
have a multiple add to cart form, which inserts into the cart array if the checkbox is checked
want to popup a message to warn if nothing is checked, before form is posted.
got this, but alert pops up everytime, even if checkbox is checked.
<script type="text/javascript">
function addToBasket(form) {
var isChecked = false;
if ( document.forms['add_to_cart'].elements['atccheck[]'].checked == true) {
isChecked = true;
}
else {
for ( var i=0; i < document.forms['add_to_cart'].elements['atccheck[]']; i++) {
if ( document.forms['add_to_cart'].elements['atccheck[]'][i].checked == true ) {
isChecked = true;
break;
}
}
}
if ( isChecked == false ) {
alert("Please select a spring. Then press 'Add to Basket'.");
}
else {
form.submit();
}
}
</script>
Jason Byrnes
06-17-2011, 09:09 AM
please send a link to your page so i can test this in the browser.
CraigR
06-17-2011, 12:20 PM
here's a link to a representative page
http://www.test3.forthwebsolutions.com/Store/test.php
click on 'find part' to show result table
Jason Byrnes
06-20-2011, 11:54 AM
client validation is problematic with checkbox group arrays, you should use server side validation on the checkbox group instead.
CraigR
06-20-2011, 12:14 PM
Ok, can i do this via the server validations interface ?
I'm not sure what criteria to select.
I tried Required/Not Blank/Selection made on atccheck[] being posted, but if i have 50 rows, i only need one to be checked for the entry to be valid.
Jason Byrnes
06-20-2011, 12:33 PM
Yes, use the required not blank validation.
select any one of the atcheck form elements, the only thing you will need to change is that the code will be added using the "[]" in the name:
$_POST["atccheck[]"])
remove the square brackets:
$_POST["atccheck"])
CraigR
06-20-2011, 12:43 PM
thanks Jason.
Sorted
Jason Byrnes
06-20-2011, 01:00 PM
you're welcome.
CraigR
06-29-2011, 02:47 AM
it would be nice to achieve this without resorting to server side validation and reloading the page.
i have looked at the documentation re implementing spry validation on checkboxes and groups of checkboxes.
i can get it to work with a simple example, but if i use a checkbox array, the validation only works on the first checkbox.
Jason Byrnes
06-29-2011, 06:58 AM
ok, try using this for the script:
<script type="text/javascript">
function addToBasket(form) {
var isChecked = false;
var chks = document.getElementsByName('atccheck[]');
if ( chks.checked == true) {
isChecked = true;
}
else {
for ( var i=0; i < chks.length; i++) {
if ( chks[i].checked == true ) {
isChecked = true;
break;
}
}
}
if ( isChecked == false ) {
alert("Please select a spring. Then press 'Add to Basket'.");
}
return isChecked;
}
</script>
and this for the submit button:
<input name="submit" type="submit" value="Add to Cart" onclick="return addToBasket()" />
CraigR
06-29-2011, 08:04 AM
Excellent.
Thanks very much, (again). Jason
Jason Byrnes
06-29-2011, 08:33 AM
you're welcome.
vBulletin® v3.8.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.