JavaScript: Only submit a form if a checkbox is checked?

Discussion in 'Computer Games and General Discussion' started by SpaceJump, Jan 12, 2010.

  1. SpaceJump
    OP

    Member SpaceJump Where's my new 2D-Metroid?

    Joined:
    Aug 18, 2005
    Messages:
    3,648
    Location:
    Zebes
    Country:
    Germany
    I want a form to only be submitted if a checkbox is checked. This is what I have so far:

    The function:
    CODE
    ÂÂ


    The HTML-Form:
    CODE
    ...
    Checkbox

    Now if the checkbox is not checked, the message "Checkbox not activated!" comes, but the form is still submitted. How can I prevent this?
     
  2. nIxx

    Member nIxx GBAtemp Maniac

    Joined:
    Sep 30, 2007
    Messages:
    1,459
    Location:
    Germany
    Country:
    Germany
    It´s been a while that i worked with javascript but i think you need something like this



    function check()
    {
    if (document.formular.theCheckbox.checked)
    {
    alert('Send Form');
    return true;
    }
    else if (...)
    {
    alert('Not checked...');
    return false;
    }

    return false;
    }

    if onSubmit gets true it should send if false not
     
  3. camurso_

    Member camurso_ GBAtemp Advanced Fan

    Joined:
    Dec 23, 2004
    Messages:
    540
    Country:
    Portugal
    That is correct.

    If onSubmit returns false, the form won´t be send to the server. All you have to do is to create a function that is called onSubmit and it should return true/false. The event must return, also, true/false.
     
  4. SpaceJump
    OP

    Member SpaceJump Where's my new 2D-Metroid?

    Joined:
    Aug 18, 2005
    Messages:
    3,648
    Location:
    Zebes
    Country:
    Germany
    Just tried it and it worked! Thanks [​IMG]
     

Share This Page