Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currently I have a simple fieldset and I would like to be able to write a function that checks if all the inputs are filled out, and if so collapses into the legend. If one ore more of the input fields are empty the fieldset stays open. I know how to make it collapse into the legend I just need help with the validation function.

HTML
<fieldset>
        <legend><a href="#" onclick="doit2()">Personal details</a></legend>
        <div id="two">
            <div>
                <label>Surname or family name:</label>
                <input type="text" name="personal"/>
            </div>
            <div>
                <label>Given name/names:</label>
                <input type="text" name="personal"/>
            </div>
            <div>
                <label> Date of birth:</label>
                <input type="date" name="personal"/>
            </div>
            <div>
                <label> Male </label>
                <input type="radio" name="gender" value="Male"/>
            </div>
            <div>
                <label> Female </label>
                <input type="radio" name="gender" value="Female"/>
            </div>
            <div>
                <label> N/A </label> 
                <input type="radio" name="gender" value="NA"/>
            </div>
        </div>
    </fieldset>


What I have tried:

JavaScript
function doit2() {
       if(document.getElementById('two').style.display == 'none'){
           document.getElementById('two').style.display = 'block';
       } else {
           document.getElementById('two').style.display = 'none';
       }
   }
Posted
Updated 27-May-17 9:22am

1 solution

Hi
In simple way you can write the same in JavaScript. I just adding sample code for input type text you may add in same for other field type as well.

JavaScript
function doit2() {
            var flagEmpty = false;
            $("#two").find("input[type='text']").each(function () {
                if ($(this).val().trim() == "") {
                    flagEmpty= true;
                }
            })
            if (!flagEmpty) {
                if (document.getElementById('two').style.display == 'none') {
                    document.getElementById('two').style.display = 'block';
                } else {
                    document.getElementById('two').style.display = 'none';
                }
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900