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

D

DoomedLung

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 :)
 
R

RobG

DoomedLung said:
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 :)
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top