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

Discussion in 'Javascript' started by DoomedLung, Sep 28, 2006.

  1. DoomedLung

    DoomedLung Guest

    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 :)
    DoomedLung, Sep 28, 2006
    #1
    1. Advertising

  2. DoomedLung

    RobG Guest

    Re: Ensure at least one checkbox or input element is checked or notempty

    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
    RobG, Sep 28, 2006
    #2
    1. Advertising

  3. DoomedLung

    DoomedLung Guest

    Cheers :)
    DoomedLung, Sep 28, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yurps
    Replies:
    1
    Views:
    6,887
    rajeshthangarasu1983
    Feb 29, 2008
  2. Lukas
    Replies:
    3
    Views:
    805
    spiff
    Nov 10, 2005
  3. dman
    Replies:
    1
    Views:
    725
    Joseph Kesselman
    Oct 23, 2007
  4. NotGiven
    Replies:
    5
    Views:
    172
    Thomas 'PointedEars' Lahn
    Feb 12, 2004
  5. Zvonko Bi¹kup

    Validate that at least one checkbox is checked

    Zvonko Bi¹kup, Mar 13, 2007, in forum: Javascript
    Replies:
    2
    Views:
    409
    -Lost
    Mar 13, 2007
Loading...

Share This Page