----- Original Message -----
From: "Ian Vaughan" <[EMAIL PROTECTED]>
> I am trying to add javascipt validation to my form fields before the data
is
> submitted to the database via coldfusion, however it is not working
> correctly and would appreciate any help anybody on the list can give.
>
> The form.area and form.dlos are select boxes, and these are the major
> problem how can these be validated so that the user must select an option
> from the list ??
----------------------------
Haven't tried to debug your code, but I thought these might be helpful, the
set of JS functions I use for client-side validation of select boxes:
----------------------------
/*
This checks to ensure that no less than a minimum number of options
have been selected in a multiple select box
formName = the name of the form
minChecks = 2-dimensional array
- First dimension contains list of fields to be
checked
- Second dimension contains corresponding minimum
number of
options to be selected for each one
*/
function isMinSelected(formName,minChecks) {
for (element=0;element<minChecks.length;element++) {
var field = eval('document.forms[formName].'+minChecks[element][0]);
var check = minChecks[element][1];
if (optionsSelected(formName,minChecks[element][0])<check) {
alert('The select box \''+minChecks[element][0]+'\' must have at least
'+check+' options selected. Sort it out!');
field.focus();
return false;
}
}
return true;
}
/*
This checks to ensure that no more than a maximum number of options
have been selected in a multiple select box
formName = the name of the form
maxChecks = 2-dimensional array
- First dimension contains list of fields to be
checked
- Second dimension contains corresponding maximum
number of
options to be selected for each one
*/
function isMaxSelected(formName,maxChecks) {
for (element=0;element<maxChecks.length;element++) {
var field = eval('document.forms[formName].'+maxChecks[element][0]);
var check = maxChecks[element][1];
if (optionsSelected(formName,maxChecks[element][0])>check) {
alert('The select box \''+maxChecks[element][0]+'\' cannot have more than
'+check+' options selected. Sort it out!');
field.focus();
return false;
}
}
return true;
}
/*
Counts how many options are selected in a multiple select box
formName = the name of the form
elementName = the name of the select box element
*/
function optionsSelected(formName,elementName) {
var totalSelected = 0;
var field = eval('document.forms[formName].'+elementName);
for (option=0;option<field.length;option++) {
if (field.options[option].selected) {
totalSelected++;
}
}
return totalSelected;
}
------------------------------------
HTH,
- Gyrus
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- [EMAIL PROTECTED]
work: http://www.tengai.co.uk
play: http://www.norlonto.net
- PGP key available
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______________________________________________________________________
Your ad could be here. Monies from ads go to support these lists and provide more
resources for the community. http://www.fusionauthority.com/ads.cfm
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists