Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

required field within a jquery accordion

Basic form using a JQuery Accordion and attempting to validate required fields. Am I missing something simple? Thanks!

javascript:

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $("#accordion").accordion();
        $("#Form1").validate({
            rules: {
                AccordionField: { required: true }
            }
        });
    });    
</script>

html:

<form id="Form1" action="foo.htm">
    <div id="accordion">
    <h3><a href="#">Section 1</a></h3>
    <div><p>content</p></div>
    <h3><a href="#">Section 2</a></h3>
    <div><p><input id="AccordionField" class="required" /></p></div>
    </div>
    <p><button type="submit">Submit</button></p>
</form>

Problem: If the second section is collapsed during the submit, validation does not validate the required field.

like image 843
pramirez Avatar asked Mar 16 '12 13:03

pramirez


1 Answers

jQuery validate (1.9) ignores hidden fields by default. You can override this behavior using the ignore option:

$(document).ready(function() {
    $("#accordion").accordion();
    $("#Form1").validate({
        rules: {
            AccordionField: {
                required: true
            }
        },
        ignore: []
    });
});​

Example: http://jsfiddle.net/andrewwhitaker/VWWkP/

like image 65
Andrew Whitaker Avatar answered Nov 07 '22 10:11

Andrew Whitaker