formvalidation on a checkbox

N

nescio

hello,

i have a form, that contains only 5 checkboxes, named box[1], box[2] etc.
there are no other textfields than this five checkboxes;

people have to check at least one checkbox;

i want to do a form validation but it does not work;
this code normaly works on textfields without a problem.

here is the javascript source code i tried:

---------------------- source
code ---------------------------------------------
function formMaakCSV(hetForm){
if (hetForm.box[1].value='' || hetForm.box[2].value=''||
hetForm.box[3].value=''|| hetForm.box[4].value=''||
hetForm.box[5].value=''){
alert("U moet minimaal een checkbox invullen");
return (false);
}

return (true);
}
 
Z

Zwerfkat

nescio said:
hello,

i have a form, that contains only 5 checkboxes, named box[1], box[2] etc.
there are no other textfields than this five checkboxes;

people have to check at least one checkbox;

i want to do a form validation but it does not work;
this code normaly works on textfields without a problem.

here is the javascript source code i tried:

---------------------- source
code ---------------------------------------------
function formMaakCSV(hetForm){
if (hetForm.box[1].value='' || hetForm.box[2].value=''||
hetForm.box[3].value=''|| hetForm.box[4].value=''||
hetForm.box[5].value=''){
alert("U moet minimaal een checkbox invullen");
return (false);
}

return (true);
}

Probably you have to use the array indexes 0..4 instead of 1..5.

Or use the following code which works for every amount of check boxes:

function formMaakCSV(hetForm){
for(i=0;i<hetForm.box.length;i++)
if(hetForm.box.value != '') return (true);
alert("U moet minimaal een checkbox invullen");
return (false);
}

// Groeten uit Soest!
 
G

Gomolyako Eduard

nescio пиÑал(а):
hello,

i have a form, that contains only 5 checkboxes, named box[1], box[2] etc.
there are no other textfields than this five checkboxes;

people have to check at least one checkbox;

i want to do a form validation but it does not work;
this code normaly works on textfields without a problem.

here is the javascript source code i tried:

---------------------- source
code ---------------------------------------------
function formMaakCSV(hetForm){
if (hetForm.box[1].value='' || hetForm.box[2].value=''||
hetForm.box[3].value=''|| hetForm.box[4].value=''||
hetForm.box[5].value=''){
alert("U moet minimaal een checkbox invullen");
return (false);
}

return (true);
}

Try to validate by "checked" property of checkboxes.

Best, Ed.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri,
28 Oct 2005 13:38:22, seen in nescio
i have a form, that contains only 5 checkboxes, named box[1], box[2] etc.
there are no other textfields than this five checkboxes;
people have to check at least one checkbox;
function formMaakCSV(hetForm){
if (hetForm.box[1].value='' || hetForm.box[2].value=''||
hetForm.box[3].value=''|| hetForm.box[4].value=''||
hetForm.box[5].value=''){
alert("U moet minimaal een checkbox invullen");
return (false);
}

return (true);
}

Consider

function formMaakCSV(hetForm) { var X = 0
for (var J=0 ; J<5 ; J++) X += + hetForm.box[J].checked
if (!X) alert("U moet minimaal een checkbox invullen")
return !!X }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top