Validate Individual Sections of Accordion

This article explains how to validate individual sections of an accordion.

Introduction

In my previous article I explained How to Create an Accordion that is having a feature to Open/Close Sections With/Without clicking on them.

As I said in my previous article, you can make your accordion in such a way that each section will only be closed when it is in a valid form (has valid data).

I will say the same thing in other words, not allowing the user to open/close sections of the Accordion until it's in a valid form.

So, let's see the procedure to create such an accordion.

Step 1

If you can remember from my previous article, we created some divs that were the sections of the accordion. So in today's case we need to validate those sections, in other words the divs that are not possible until you use any kind of external plugin (or it might be possible but unknown to me :) ), so to remove this problem I make some small changes in my both jQuery and HTML code and everything began working. I changed those divs with a <form> tag. Now the question is, why did I do that? The answer is, you can validate your forms very easily rather than validating anything else. So, change all those divs with <form> like this:

  1. <div class="slider">  
  2.         <form id="1divSection" class="SectionClass hide" style="border:1px thin black">  
  3.             <h3 class="bg-danger">Click Here to Open First Tab </h3>  
  4.             <div class="container">  
  5.                 <div class="col-lg-12 col-md-12">  
  6.                     <div class="form-group hide" id="Div1">  
  7.                         Provide Your First Name :- <input type="text" required/>  
  8.                         <br />  
  9.                         <br />  
  10.                         Provide Your Last Name  :- <input type="text" required/>  
  11.                     </div>  
  12.                 </div>  
  13.                 <div class="clearfix"></div>  
  14.                 <button type="button" class="btn btn-primary btn-lg active">Next</button>  
  15.                 <div class="clearfix"></div>  
  16.             </div>  
  17.         </form>  
  18.         <form id="2divSection" class="SectionClass hide" style="border:1px thin black">  
  19.             <h3 class="bg-danger">Cick Here to Open Second Tab </h3>  
  20.             <div class="container">  
  21.                 <div class="col-lg-12 col-md-12">  
  22.                     <div class="form-group hide" id="Div2">                          
  23.                         Provide Your Mobile Number:- <input type="number" />  
  24.                         <br />  
  25.                         <br />  
  26.                         Provide Your Email ID        :- <input type="email" />  
  27.                     </div>  
  28.                 </div>  
  29.                 <div class="clearfix"></div>  
  30.                 <button type="button" class="btn btn-primary btn-lg active">Next</button>  
  31.                 <div class="clearfix"></div>  
  32.             </div>  
  33.         </form>  
  34.         <form id="Div3" class="SectionClass hide" style="border:1px thin black">  
  35.             <h3 class="bg-danger">Cick Here to Open Third Tab </h3>  
  36.             <div class="container">  
  37.                 <div class="col-lg-12 col-md-12">  
  38.                     <div class="form-group hide" id="Div4">                          
  39.                         Provide Your Mailing Address  :- <input type="text" required/>  
  40.                         <br />  
  41.                         <br />  
  42.                         Provide Your Permanent Address :- <input type="text" />  
  43.                     </div>  
  44.                 </div>  
  45.                 <div class="clearfix"></div>  
  46.                 <button type="button" class="btn btn-primary btn-lg active">Next</button>  
  47.                 <div class="clearfix"></div>  
  48.             </div>  
  49.         </form>  
  50.         <form id="Div5" class="SectionClass hide" style="border:1px thin black">  
  51.             <h3 class="bg-danger">Cick Here to Open Fourth Tab </h3>  
  52.             <div class="container">  
  53.                 <div class="col-lg-12 col-md-12">  
  54.                     <div class="form-group hide" id="Div6">                          
  55.                         Provide Your Mother Name :- <input type="text" required/>  
  56.                         <br />  
  57.                         <br />  
  58.                         Provide Your Father Name :- <input type="text" required/>  
  59.                     </div>  
  60.                 </div>  
  61.                 <div class="clearfix"></div>  
  62.                 <button type="button" class="btn btn-primary btn-lg active">Next</button>                  
  63.                 <div class="clearfix"></div>  
  64.             </div>  
  65.         </form>  
  66.         <form id="Div7" class="SectionClass hide" style="border:1px thin black">  
  67.             <h3 class="bg-danger">Cick Here to Open Fifth Tab </h3>  
  68.             <div class="container">  
  69.                 <div class="col-lg-12 col-md-12">  
  70.                     <div class="form-group hide" id="Div8">                          
  71.                         Provide Your Current CTC  :- <input type="text" />  
  72.                         <br />  
  73.                         <br />  
  74.                         Provide Your Expected CTC :- <input type="text" />  
  75.                     </div>  
  76.                 </div>  
  77.                 <div class="clearfix"></div>  
  78.             </div>  
  79.         </form>  
  80.     </div> 

Step 2

Now we need to check whether our current form is valid or not. This could only be done on the click of each button present in each form and we are already having a button so why not use it? So, I just make a small change in the click event of these buttons as in the following:

  1. $('.btn-primary').click(function () {  
  2.                 if ($(this).parent().parent().valid()) {                   
  3.                     $(this).parent().slideToggle("fast").parent().siblings().children(".container:visible").slideUp("fast");  
  4.                     $(this).parent().siblings().toggleClass("current");  
  5.                     $(this).parent().siblings().siblings("h3").addClass("current");  
  6.                     $(this).parent().siblings().parent().next().children('h3').next(".container").slideToggle("fast").parent()
                         .siblings().children(
    ".container:visible").slideUp("fast");  
  7.                     $(this).parent().siblings().parent().next().children('h3').toggleClass("current");  
  8.                     $(this).parent().siblings().parent().next().children('h3').siblings("h3").removeClass("current");  
  9.                     return false;  
  10.                 }  
  11.             });

So, from now on you next Section will expand only when this current section is valid.

But still we have a problem, if the user clicks on the head of a section. In that case the accordion will work normally and your form will not be checked, whether it's valid or not, so the simple solution is "Comment the code of click event of <h3> tag :D".

  1. //$('.slider h3').click(function () {  
  2.             //    $(this).next(".container").slideToggle("fast").parent().siblings().children(".container:visible")
                        .slideUp("fast");
      
  3.             //    $(this).toggleClass("current");  
  4.             //    $(this).siblings("h3").removeClass("current");  
  5.             //    return false;  
  6.             //}); 

Step 3

Now one more problem develops. What would happen if the user wants to update a section? Because he does not have an option to open the section again (we removed the code of the click event for the <h3> tag) so one solution is to provide an edit button on all those sections that are valid and filled in. This can be done by adding a button inside <h3> tag and making it display:none.

  1. <form id="1divSection" class="SectionClass hide" style="border:1px thin black">  
  2.             <h3 class="bg-danger">Click Here to Open First Tab   
  3.                 <input type="button" class="clsEdit" style="display:none" value="Edit" />  
  4.             </h3>  
  5.             <div class="container">  
  6.                 <div class="col-lg-12 col-md-12">  
  7.                     <div class="form-group hide" id="Div1">  
  8.                         Provide Your First Name :- <input type="text" required/>  
  9.                         <br />  
  10.                         <br />  
  11.                         Provide Your Last Name  :- <input type="text" required/>  
  12.                     </div>  
  13.                 </div>  
  14.                 <div class="clearfix"></div>  
  15.                 <button type="button" class="btn btn-primary btn-lg active">Next</button>  
  16.                 <div class="clearfix"></div>  
  17.             </div>  
  18.         </form>

Now we need to enable it on the submission of each form, so again we need to make changes on a button click.

  1. $('.btn-primary').click(function () {  
  2.     if ($(this).parent().parent().valid()) {  
  3.         $(this).parent().siblings().children().attr('style''display:block inline');  
  4.         $(this).parent().slideToggle("fast").parent().siblings().children(".container:visible").slideUp("fast");  
  5.         $(this).parent().siblings().toggleClass("current");  
  6.         $(this).parent().siblings().siblings("h3").addClass("current");  
  7.         $(this).parent().siblings().parent().next().children('h3').next(".container").slideToggle("fast").parent().siblings()
  8.          .children(".container:visible").slideUp("fast");  
  9.         $(this).parent().siblings().parent().next().children('h3').toggleClass("current");  
  10.         $(this).parent().siblings().parent().next().children('h3').siblings("h3").removeClass("current");  
  11.         return false;  
  12.     }  
  13. }); 

Now only that button will become visible whose parent form is valid and all the others will still be in hidden form.

Step 4

Now we need to work on the click event of this new button because we need to open the section on it's click.

  1. $('.clsEdit').click(function () {  
  2.         $(this).parent().next(".container").slideToggle("fast").parent().siblings().children(".container:visible").slideUp("fast");  
  3.         $(this).parent().toggleClass("current");  
  4.         $(this).parent().siblings("h3").removeClass("current");  
  5. }); 

But now you open the scope for your testing team because they might say "If we are clicking on this edit button again then it's making the section close, if it's for edit then how it can close the Section? :D ". So, play safe and add one more line to the click event of this button as in the following:

  1. $('.clsEdit').click(function () {  
  2.         $(this).parent().next(".container").slideToggle("fast").parent().siblings().children(".container:visible").slideUp("fast");  
  3.         $(this).parent().toggleClass("current");  
  4.         $(this).parent().siblings("h3").removeClass("current");  
  5.         $(this).attr('style''display:none');  
  6. }); 

This will be make your Edit button hidden again so now that question will not develop.

Now our application is created and it's complete code looks something like this:

  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.     <script src="jquery-1.6.4.js"></script>  
  6.     <link href="bootstrap-theme.css" rel="stylesheet" />  
  7.     <script src="jquery.validate.min.js"></script>  
  8.     <script>  
  9.         $(document).ready(function () {  
  10.             $(".slider div div").show();  
  11.             setTimeout("$('.slider').children('form').children('div').slideToggle('fast');", 1000);  
  12.             $('.SectionClass:visible').first().children('div').slideToggle('fast');  
  13.             //$('.slider h3').click(function () {  
  14.             //    $(this).next(".container").slideToggle("fast").parent().siblings().children(".container:visible")
  15.                   .slideUp("fast");  
  16.             //    $(this).toggleClass("current");  
  17.             //    $(this).siblings("h3").removeClass("current");  
  18.             //    return false;  
  19.             //});  
  20.             $('.btn-primary').click(function () {  
  21.                 if ($(this).parent().parent().valid()) {  
  22.                     $(this).parent().siblings().children().attr('style''display:block inline');  
  23.                     $(this).parent().slideToggle("fast").parent().siblings().children(".container:visible").slideUp("fast");  
  24.                     $(this).parent().siblings().toggleClass("current");  
  25.                     $(this).parent().siblings().siblings("h3").addClass("current");  
  26.                     $(this).parent().siblings().parent().next().children('h3').next(".container").slideToggle("fast").parent()
  27.                      .siblings().children(".container:visible").slideUp("fast");  
  28.                     $(this).parent().siblings().parent().next().children('h3').toggleClass("current");  
  29.                     $(this).parent().siblings().parent().next().children('h3').siblings("h3").removeClass("current");  
  30.                     return false;  
  31.                 }  
  32.             });  
  33.             $('.clsEdit').click(function () {  
  34.                     $(this).parent().next(".container").slideToggle("fast").parent().siblings().children(".container:visible")
  35.                      .slideUp("fast");  
  36.                     $(this).parent().toggleClass("current");  
  37.                     $(this).parent().siblings("h3").removeClass("current");  
  38.                     $(this).attr('style''display:none');  
  39.             });  
  40.         });  
  41.     </script>  
  42. </head>  
  43. <body>  
  44.     <div class="slider">  
  45.         <form id="1divSection" class="SectionClass hide" style="border:1px thin black">  
  46.             <h3 class="bg-danger">Click Here to Open First Tab   
  47.                 <input type="button" class="clsEdit" style="display:none" value="Edit" />  
  48.             </h3>  
  49.             <div class="container">  
  50.                 <div class="col-lg-12 col-md-12">  
  51.                     <div class="form-group hide" id="Div1">  
  52.                         Provide Your First Name :- <input type="text" required/>  
  53.                         <br />  
  54.                         <br />  
  55.                         Provide Your Last Name  :- <input type="text" required/>  
  56.                     </div>  
  57.                 </div>  
  58.                 <div class="clearfix"></div>  
  59.                 <button type="button" class="btn btn-primary btn-lg active">Next</button>  
  60.                 <div class="clearfix"></div>  
  61.             </div>  
  62.         </form>  
  63.         <form id="2divSection" class="SectionClass hide" style="border:1px thin black">  
  64.             <h3 class="bg-danger">Cick Here to Open Second Tab   
  65.                 <input type="button" class="clsEdit" style="display:none" value="Edit" />  
  66.             </h3>  
  67.             <div class="container">  
  68.                 <div class="col-lg-12 col-md-12">  
  69.                     <div class="form-group hide" id="Div2">                          
  70.                         Provide Your Mobile Number:- <input type="number" />  
  71.                         <br />  
  72.                         <br />  
  73.                         Provide Your Email ID        :- <input type="email" />  
  74.                     </div>  
  75.                 </div>  
  76.                 <div class="clearfix"></div>  
  77.                 <button type="button" class="btn btn-primary btn-lg active">Next</button>  
  78.                 <div class="clearfix"></div>  
  79.             </div>  
  80.         </form>  
  81.         <form id="Div3" class="SectionClass hide" style="border:1px thin black">  
  82.             <h3 class="bg-danger">Cick Here to Open Third Tab  
  83.                 <input type="button" class="clsEdit" style="display:none" value="Edit" />  
  84.             </h3>  
  85.             <div class="container">  
  86.                 <div class="col-lg-12 col-md-12">  
  87.                     <div class="form-group hide" id="Div4">                          
  88.                         Provide Your Mailing Address  :- <input type="text" required/>  
  89.                         <br />  
  90.                         <br />  
  91.                         Provide Your Permanent Address :- <input type="text" />  
  92.                     </div>  
  93.                 </div>  
  94.                 <div class="clearfix"></div>  
  95.                 <button type="button" class="btn btn-primary btn-lg active">Next</button>  
  96.                 <div class="clearfix"></div>  
  97.             </div>  
  98.         </form>  
  99.         <form id="Div5" class="SectionClass hide" style="border:1px thin black">  
  100.             <h3 class="bg-danger">Cick Here to Open Fourth Tab  
  101.                 <input type="button" class="clsEdit" style="display:none" value="Edit" />  
  102.             </h3>  
  103.             <div class="container">  
  104.                 <div class="col-lg-12 col-md-12">  
  105.                     <div class="form-group hide" id="Div6">                          
  106.                         Provide Your Mother Name :- <input type="text" required/>  
  107.                         <br />  
  108.                         <br />  
  109.                         Provide Your Father Name :- <input type="text" required/>  
  110.                     </div>  
  111.                 </div>  
  112.                 <div class="clearfix"></div>  
  113.                 <button type="button" class="btn btn-primary btn-lg active">Next</button>                  
  114.                 <div class="clearfix"></div>  
  115.             </div>  
  116.         </form>  
  117.         <form id="Div7" class="SectionClass hide" style="border:1px thin black">  
  118.             <h3 class="bg-danger">Cick Here to Open Fifth Tab  
  119.                 <input type="button" class="clsEdit" style="display:none" value="Edit" />  
  120.             </h3>  
  121.             <div class="container">  
  122.                 <div class="col-lg-12 col-md-12">  
  123.                     <div class="form-group hide" id="Div8">                          
  124.                         Provide Your Current CTC  :- <input type="text" />  
  125.                         <br />  
  126.                         <br />  
  127.                         Provide Your Expected CTC :- <input type="text" />  
  128.                     </div>  
  129.                 </div>  
  130.                 <div class="clearfix"></div>  
  131.             </div>  
  132.         </form>  
  133.     </div>  
  134. </body>  
  135. </html> 

Output

On running the application you will see a normal Accordion just as in my previous article.

jQueryAccordion

If you click on the button without providing the valid values then you will get an error message like this:

jQueryAccordion

Provide valid values and again click on the button to see the real changes. You will see that this section is closed and the next section becomes visible and this closed section has an edit button.

jQueryAccordion

If you click on the edit button on any section then that section will be opened and the edit button will become hidden again.

jQueryAccordion