Solved

JavaScript validation - assistance

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

713 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