1

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. I am calling the function as follows:

4
  • How are you calling the function? Commented Apr 15, 2010 at 13:42
  • 1
    Do you have ONE good reason why not use one of the many excellent libraries out there (hmm,...mootools...) and use their pre-built mechanizems just for that? Commented Apr 15, 2010 at 13:42
  • 1
    Because dragging in an enormous framework library to check the .checked property of two radio buttons is completely insane? Just a thought. Commented Apr 15, 2010 at 13:53
  • Well @bobince you're right if that's really all he's doing, but it's a safe bet that there's a bunch of code doing other stuff. Commented Apr 15, 2010 at 14:03

2 Answers 2

5

Sounds like your form isn't stopping when it should be. I assume you have something like

 <form onsubmit="validate_radio()">...</form>

Since your validate_radio() function returns false on failure, you just need to modify your form to fail if the validation does:

<form onsubmit="return validate_radio()">...</form>

So the form will halt if validation fails.

Sign up to request clarification or add additional context in comments.

3 Comments

LOL, +1 my answer (a few seconds later) was almost identical.
@OP: The one thing my answer had that this doesn't: Why not just make one of the buttons pre-selected? Then you'll know that there's always one selected, no validation required...
:D I need the rep, you definitely don't!
0

For what it's worth, I think that it's not good for a form to include radio buttons that don't start off with one being selected. Young people don't remember old radios, or being yelled at for messing around with the buttons to get them all to pop out. Radio buttons are called "radio buttons" because one of them must always be selected. It's friendlier for your users to have the buttons initialized to a good default setting.

1 Comment

I totally never made that connection... I remember "tricking" the radio into breaking, was always fun :D Your post made me lol :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.