PDA

View Full Version : Checkbox and radio button validation


dominicall
11-08-2002, 10:59 PM
Evening all...

Am having a bit of trouble understanding this (am VB/SQL prog) so thought I'd ask for help.

Am using a form checking script that I found on www.dithered.com - by and large it's very easy to use and very complete... however, it seems to ignore checkboxes and radio buttons - the radio script was included in the original from dithered and I adapted for checkboxes.

To call the overall checking function, you use the folliwing:
details.search.required = true;
details.search.requiredError = 'The search field must be completed.';
Then, if required is false on the check the requiredError value is added to the overall check string and all errors are shown to the user when the form is submitted.

The code for the radio button and checkboxes which seems to be ignored is:
// radio buttons
else if (element.type == "radio") {
var radiogroup = [element.name];

// required element
if (radiogroup.required && radiogroup.length) {
var checkedRadioButton = -1;
for (var radioIndex = 0; radioIndex < radiogroup.length; radioIndex++) {
if (radiogroup[radioIndex].checked == true) {
checkedRadioButton = radioIndex;
break;
}
}
if (checkedRadioButton == -1 && !radiogroup.tested) {
errors[errors.length] = radiogroup.requiredError;
radiogroup.tested = true;
}
}

radiogroup = null;
}

// checkboxes
else if (element.type == "checkbox") {
var checkgroup = [element.name];

// required element
if (checkgroup.required && checkgroup.length) {
var checkedCheckbox = -1;
for (var checkIndex = 0; checkIndex < checkgroup.length; checkIndex++) {
if (checkgroup[checkIndex].checked == true) {
checkedCheckbox = checkIndex;
break;
}
}
if (checkedCheckbox == -1 && !checkgroup.tested) {
errors[errors.length] = checkgroup.requiredError;
checkgroup.tested = true;
}
}

checkgroup = null;
}

I've attached the full script js file as .txt file as well. Credit to Chris Nott at www.dithered.com for the original work.

Anyone any ideas why when I call .required for checkboxes and/or radio buttons it's being ignored???

Dominic :D

beetle
11-09-2002, 02:37 AM
I'd be super-pleased if you gave fValidate (http://www.peterbailey.net/fValidate/site.php) a lookover. The checkbox and radio-button validators are in place, but a bit buggy across ALL platforms. However, I'm releasing a new version in a couple days that will fix that.