views:

60

answers:

1

I'm using the jQuery validation plugin from bassistance.de. It works fine.

From <head>:

<script type="text/javascript" src="/static/JQuery.js"></script>
<script type="text/javascript" src="/static/js-lib/jquery.validate.pack.js"></script>
<script type="text/javascript" src="/static/js-lib/jquery.validate.additional-methods.js"></script>

At first, this was the only validation code I had, and it worked:

$("form").validate();
$("#form-username").rules("add", {
    required: true,
    email: true,
});

It was validating this HTML:

            <form id="form-username-form" action="api/user_of_email" method="get">
                <p>
                <label for="form-username">Email:</label>
                <input type="text" name="email" id="form-username" />
                <input type="submit" value="Submit" id="form-submit" />
                </p>
            </form>

Great, everything works. But then I add this JS:

$("#form-choose-options input[type='text']").rules("add", {
    number: true,
});

to validate this markup:

            <form id="form-choose-options" action="api/set_options" method="get">
                <p>
                    <label for="form-min-credits">Min credits per term:</label><input type="text" name="min_credits" id="form-min-credits" /> <br />
                    <label for="form-optimal-credits">Optimal credits per term:</label><input type="text" name="optimal_credits" id="form-optimal-credits" /> <br />
                    <label for="form-max-credits">Max credits per term:</label><input type="text" name="max_credits" id="form-max-credits" /> <br />
                    <label for="form-low-GPA">Lowest acceptable GPA:</label><input type="text" name="low_GPA" id="form-low-GPA" /> <br />
                    <label for="form-high-GPA">Highest realistic GPA:</label><input type="text" name="high_GPA" id="form-high-GPA" /> <br />
                    <input type="hidden" class="user-pk" name="pk"/>
                    <input type="submit" value="Submit" />
                </p>
            </form>

This causes a javascript error on document load:

$.data(f.form, "validator") is undefined

The error is from the packer function.

What am I doing wrong?

+1  A: 
$("form").validate();
$("#form-username").rules("add", {
    required: true,
    email: true,
});

You have trailing commas in your objects. Eliminate the last comma in your object definitions and everything should work fine.

$("form").validate();
$("#form-username").rules("add", {
    required: true,
    email: true //Comma removed
});

And your other object...

$("#form-choose-options input[type='text']").rules("add", {
    number: true //Comma removed
});
Kenaniah