Solved

JavaScript validation - assistance

Posted on 2013-12-11
1
127 Views
Last Modified: 2013-12-11
I have a form. I want 2 fields to be required only when a radio button value equals: Closed.

The first required field is a drop down (select) called "happy". The default option value is " " like this:
<select name="happy">
<OPTION VALUE=" " </OPTION>
<OPTION VALUE="Yes">Yes</OPTION>
<OPTION VALUE="No">No</OPTION>
</select>

Open in new window


The 2nd field is called "Explain" and it is a text area, like this:
<textarea name="Explain" cols="10" rows="5">
</textarea>

Open in new window


When the radio button choice is set to "Closed" I want the 2 above to be required. The Select would require a yes or no, and the text area shouldn't be allowed to be left blank.

My radio buttons look like this:

<input type="radio" name="Status" value="Open" checked>Open
<input type="radio" name="Status" value="Closed">Closed

Open in new window


I'd like the validation to happen on form submit. Could someone assist with the javascript I would need?
0
Comment
Question by:earwig75
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39712366
Test page : http://jsfiddle.net/4GYSa/

window.onload = function() {
    document.getElementById("Submit").onclick = function() {
        var Status = document.getElementsByName("Status");
        var TextArea = document.getElementsByName("Explain")[0];
        var Select = document.getElementsByName("happy")[0]; 
        if( Status[1].checked && (Select.selectedIndex==0 || TextArea.value.replace(/\s/g,"") == "") ) {
            alert("Please choose happy and enter Explain"); 
            return false;
        }
        return true;
    }
}

Open in new window

0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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
Course of the Month10 days, 17 hours left to enroll

632 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