470,841 Members | 883 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

formvalidation on a checkbox

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);
}

----------------------------------------------------------------------------
----

thanks
Oct 28 '05 #1
4 1938

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);
}

----------------------------------------------------------------------------
----

thanks


Try to validate by "checked" property of checkboxes.

Best, Ed.

Oct 28 '05 #2

"nescio" <ne****@nescio.nes> wrote in message news:43**********************@nova.planet.nl...
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[i].value != '') return (true);
alert("U moet minimaal een checkbox invullen");
return (false);
}

// Groeten uit Soest!
Oct 28 '05 #3
it is working now,

thanks for your help
Oct 28 '05 #4
JRS: In article <43**********************@nova.planet.nl>, dated Fri,
28 Oct 2005 13:38:22, seen in news:comp.lang.javascript, nescio
<ne****@nescio.nes> posted :
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 }

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 28 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Fabri | last post: by
reply views Thread by mike | last post: by
2 posts views Thread by bebop | last post: by
5 posts views Thread by DotNetJunkies User | last post: by
2 posts views Thread by Adam Knight | last post: by
2 posts views Thread by Ceema M via DotNetMonster.com | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.