required field within a jquery accordion
Asked Answered
M

1

7

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.

Mccusker answered 16/3, 2012 at 13:25 Comment(0)
U
9

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/

Undersea answered 16/3, 2012 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.