Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 139
  • Last Modified:

JavaScript validation - assistance

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
earwig75
Asked:
earwig75
1 Solution
 
leakim971PluritechnicianCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now