You are here:

Javascript/Javascript Issue Radio Button Validation

Advertisement


Question
Hello, I am creating a form and want to validate some fields. I have written code that works for all my fields but having problem with radio buttons. I have written the following code and it works fine for testing but now where the alerts are, I simply want to "break" out of the if statements. I thought using a "break" would work but apparently you can only use a break statement with a for loop which I don't need. Can you look at the code and tell me if there is a simple solution for this? Thanks!

if (document.getElementById('comments1').checked)
  {
     alert("You selected the comment button");
  }
       else if (document.getElementById('order-status1').checked)
     {
        alert("You selected the order status button");
     }
        else if(document.getElementById('salesinquiry1').checked)
     {          
        alert("You selected the sales inquiry button");
     }
         else
        {
         alert("You must select one - Comments, Order Status or Sales Inquiry");
        }

Answer
Hi Ron,

Thanks for your question. A simple approach might be to use "document.getElementsByTagName" to  loop through the input elements, check if the type is "radio" and then see if it is checked.

Here is an example of what I am getting at:

http://jsfiddle.net/6nvaq/

I'm sure there are probably a few ways to do this, but this came to mind quickly, and is easy to implement.

Please feel free to follow-up if there are any other questions you may  have. I hope this was helpful to you.

Best Wishes,

Kevin Chisholm

http://kevinchisholm.com/

http://blog.kevinchisholm.com/

Javascript

All Answers


Ask Experts

Volunteer


Kevin Chisholm

Expertise

I can answer questions about: JavaScript, Object-Oriented JavaScipt, JSON, AJAX, jQuery, HTML, CSS

Experience

I am a Front-End Web Developer living in New York City. I have provided services to titles such as Time.com, TimeForKids.com, SportIllustrated.com, coastalliving.com, People.com, TDAmeritrade.com, and Instyle.com. Being particularly passionate about Object-Oriented JavaScript as well as Web Standards, I spend a fair amount of time writing about / teaching Front-End Web Development techniques. In my free time I enjoy reading about advanced JavaScript, as well as Web Development trends and best practices. My Blog is: http://blog.kevinchisholm.com/ JavaScript-specific blog posts can be found here: http://blog.kevinchisholm.com/category/javascript/

Organizations
NYC Front-end Developers CSS3 / HTML5 The future of front end Front-End Engineers Javascript Syracuse University Alumni Network

Education/Credentials
Syracuse University - VPA - 1983 - 1987

Past/Present Clients
cookingchanneltv.com foodnetwork.com food.com hgtv.com diynetwork.com gactv.com frontdoor.com hgtvremodels.com travelchannel.com Time.com TimeForKids.com SportIllustrated.com coastalliving.com People.com TDAmeritrade.com ramirezco.com njsga.org soccercoliseum.com superdomesports.com afeenj.org

©2016 About.com. All rights reserved.