isaac weathers
isaac weathers

Reputation: 1472

jquery.validate not showing messages

Can't seem to find out why my jquery.validate script is not showing the messages. Anyone see what I am doing wrong?

Trying to display messages in a separate div:

html:

  <form class="cmxform" id="commentForm" method="get" action="">
                <label for="vehiclePrice" class="form-control-label vpl">Vehicle Price</label>
                <input type="text" class="form-control" id="vehiclePrice" placeholder="$0" onkeypress="return isNumberKey(event)" value="25592" required />

                <label for="estimatedTaxesAndFees" class="form-control-label etfl">Estimated Taxes and Fees</label>
                <input type="text" class="form-control" id="estimatedTaxesAndFees" placeholder="$0" onkeypress="return isNumberKey(event)" value="2843" required />

              </form>

            <div id="error-note"></div>

js:

$(document).ready(function() {

$('#commentForm').validate({
    errorLabelContainer: "#error-note",
    wrapper: "li",
    onfocusout: function(element, event) {
        this.element(element);
    },
    rules: {
        vehiclePrice: 'required',
        estimatedTaxesAndFees: 'required'

    },
    messages: {
        vehiclePrice: 'Please enter vehicle price',
        estimatedTaxesAndFees: 'Please enter taxes and fees
    }
});

});

fiddle

Upvotes: 2

Views: 22953

Answers (2)

Felix
Felix

Reputation: 38102

1) Include jQuery in your fiddle

2) Add submit button to your HTML:

<input type="submit" value="Submit" />

3) Add missing ' at the end of your estimatedTaxesAndFees message:

estimatedTaxesAndFees: 'Please enter taxes and fees' // <-- Here

Updated Fiddle


You need to add name attribute to your input to get custom message here:

<input type="text" class="form-control" name="vehiclePrice" id="vehiclePrice"
<input type="text" class="form-control" name="estimatedTaxesAndFees"

Updated Fiddle

Upvotes: 5

Arun P Johny
Arun P Johny

Reputation: 388316

you had a unclosed string literal, also in the fiddle jQuery was not included

$(document).ready(function() {

    $('#commentForm').validate({
        errorLabelContainer: "#error-note",
        wrapper: "li",
        onfocusout: function(element, event) {
            this.element(element);
        },
        rules: {
            vehiclePrice: 'required',
            estimatedTaxesAndFees: 'required'

        },
        messages: {
            vehiclePrice: 'Please enter vehicle price',
            estimatedTaxesAndFees: 'Please enter taxes and fees'//closing ' was missing
        }
    });

});

Demo: Fiddle

The validation will trigger if you clear the contents of a textbox/or you add a submit button to the form/or call the valid() method of the form manually

Upvotes: 2

Related Questions