Solved

JavaScript validation - assistance

Posted on 2013-12-11
1
121 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
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now