?
Solved

JavaScript validation - assistance

Posted on 2013-12-11
1
Medium Priority
?
128 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
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…

719 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