?
Solved

Reading value of a radio button using javascript

Posted on 2007-10-05
2
Medium Priority
?
1,325 Views
Last Modified: 2008-01-09
Hi,

I have a HTML page with a form that has a bunch of controls.  Of these there are three particular controls that I am having problems with.  There is a radio button group (two radio buttons) and one text box.  The radio button group consists of two buttons that can take "Yes" or "No" values.  The user has to select one of the radio buttons and if the user selects the one that has the "Yes" value, he has to be fill in a value in the text box.  If the user selects "No", the text boxes are not mandatory.

Here is the HTML code


                        <dt><label class="mandatory">Do you want to be included in the mailing?</label></dt>
                        <dd><input class="radio" name="MailingSelf" type="radio" value="Yes" /><span>Yes. &nbsp;&nbsp;<font color="#0000FF" size="-5">If yes, please enter mailing address below</font></span> </dd>
                        <dd><input class="radio" name="MailingSelf" type="radio" value="No" />No. </dd>
                        <dt><label >Mailing Address : </label>
                        </dt>                        
                        <dd><textarea name="mailingselfaddress" cols="" rows=""></textarea></dd>

Here is the javascript that I am using when the form is submitted.

var self_mailer=false;
var self_mailer_value="";
for (counter = 0; counter < form.MailingSelf.length; counter++)
      {
      // If a radio button has been selected it will return true
      // (If not it will return false)
      if (form.MailingSelf[counter].checked)
            self_mailer = true;
            self_mailer_value=form.MailingSelf[counter].value;
      }
      if (!self_mailer)

      {
            alert("Please Select If You Want To Be Included In The Mailing");
            form.MailingSelf.focus();
            return;
      }
      
      if (self_mailer_value=="Yes" && form.mailingselfaddress.value="")
      {
            alert("Please enter the address that you want to include in the mailing");
            form.mailingselfaddress.focus();
            return;            
      }
      

This is not working.  It will prompt be to select a radio button if it is not selected but once I select "Yes", it is not forcing me to fill in the text box.  Why is that?  Is there anything I can do to fix it.

0
Comment
Question by:dsr79
2 Comments
 
LVL 4

Accepted Solution

by:
Russell2566 earned 2000 total points
ID: 20023043
This should work for you. I wrote the whole thing out as there were several issues. One of them being an "=" sign in your if statment instead of a "=="

<script>
      function validate(form) {
            var n;
            var self_mailer = null;

            for (n=0; n < form.MailingSelf.length; n++)
                  if (form.MailingSelf[n].checked)
                        self_mailer = form.MailingSelf[n].value;

            if (!self_mailer) {
                  alert("Please Select If You Want To Be Included In The Mailing");
                  form.submit();
            }

            if (self_mailer == "Yes" && form.mailingselfaddress.value.length == 0) {
                  alert("Please enter the address that you want to include in the mailing");
                  form.mailingselfaddress.focus();
                  return;            
            }
      }
</script>

<form name="myForm">
      <dt><label class="mandatory">Do you want to be included in the mailing?</label></dt>
      <dd><input class="radio" name="MailingSelf" type="radio" value="Yes" /><span>Yes. &nbsp;&nbsp;<font color="#0000FF" size="-5">If yes, please enter mailing address below</font></span> </dd>
      <dd><input class="radio" name="MailingSelf" type="radio" value="No" />No. </dd>
      <dt><label >Mailing Address : </label>
      </dt>                        
      <dd><textarea name="mailingselfaddress" cols="" rows=""></textarea></dd>
      <input type="button" onClick="validate(this.form);" value="submit">
</form>
0
 

Author Comment

by:dsr79
ID: 20024163
Hi Russell,

with one minor change (removing form.submit) it works great...Thanks.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question