Javascript validation for radio button

Posted by Priyanka on Stack Overflow See other posts from Stack Overflow or by Priyanka
Published on 2010-04-15T13:40:44Z Indexed on 2010/04/15 13:43 UTC
Read the original article Hit count: 335

Filed under:
|

Hello.I am doing newsletter subscription.I have 2radio buttons-subscribe and unsubscibe and a submit button.But when I click on submit button,ajax function gets called for subscription.Now i want to do validation.I have written a javascript validation for radio buttons as below:

function validate_radio()
              {
                    var radio_choice = false;
                    var radio_val = document.newsletterform.subscribe.length;
                    for (counter = 0; counter < radio_val; counter++)
                    {
                        if (document.newsletterform.subscribe[counter].checked)
                        radio_choice = true; 
                    }
                    if (!radio_choice)
                    {
                        document.getElementById("mandatory").innerHTML="Select Subscribe/Unsubscribe";
                        return false;
                    }

               }

But now I am getting the validate message but at the same time i am getting subscribed. tell me a way so that i can stop the subscription being done if the function returns false.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about php5