471,351 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,351 software developers and data experts.

Ensure at least one checkbox or input element is checked or not empty

Hey,

I'm currently developing an online work brief, where the user has to
enter details regarding the project into a form. I've come to a section
in the form where the user has the option of selecting between two
checkboxes and an input element. I have to ensure that at least one of
the checkboxes have been checked or that the input element is not
empty. I've come up with this function:

function validateMedia(){
var count = 0;
if(document.workBrief.mediaOnline.checked){
count++;
}
if(document.workBrief.mediaPrint.checked){
count++;
}
if(document.workBrief.mediaOther.value != ""){
count++;
}
if(count 0){
return true;
}else{
alert('Please choose at least one media option');
return false;
}
}

I'm sure there is a more effiecient way of writing this.

You thoughts are much appriciated :)

Sep 28 '06 #1
2 1715
DoomedLung wrote:
Hey,

I'm currently developing an online work brief, where the user has to
enter details regarding the project into a form. I've come to a section
in the form where the user has the option of selecting between two
checkboxes and an input element. I have to ensure that at least one of
the checkboxes have been checked or that the input element is not
empty. I've come up with this function:

function validateMedia(){
var count = 0;
if(document.workBrief.mediaOnline.checked){
count++;
}
if(document.workBrief.mediaPrint.checked){
count++;
}
if(document.workBrief.mediaOther.value != ""){
count++;
}
if(count 0){
return true;
}else{
alert('Please choose at least one media option');
return false;
}
var f = document.workBrief;
if ( !f.mediaOnline.checked &&
!f.mediaPrint.checked &&
f.mediaOther.value == ""){
alert('Please choose at least one media option');
return false;
}
}

I'm sure there is a more effiecient way of writing this.
Maybe the above is :-)
--
Rob
Sep 28 '06 #2
Cheers :)

Sep 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jason Dykes | last post: by
4 posts views Thread by Piotr | last post: by
29 posts views Thread by Amer Neely | last post: by
reply views Thread by msg4james | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.