0

Im trying to add a custom validation method to jquery validation plugin. When I try to attach the rule to an element I receive an error "uniqueTicket" not defined.

Here is my add method:

jQuery.validator.addMethod("uniqueTicket", function (value, element, param) {

    var data = {
        ticket: value,
        reservation_id: $("#reservation_id").val()
    };

    if (value.length > 3) {
        $.ajax({
            type: "POST",
            url: '/ticketaudits/validateTicket',
            data: data,
            dataType: "json",
            success: function (msg) {
                //If username exists, set response to true
                response = (msg == 'true') ? true : false;
            }
        });
    } else {
        response = false;
    }
    return response;
},
    "Ticket is invalid or already used");

This is being called within the same document ready function as the validation. My validation is:

$('#myForm').validate({
    debug: false,
    ignoreTitle: true,
    onkeyup: false,
    rules: {
        ticket: uniqueTicket
    }
});

I've followed a number of examples for this but don't understand the problem. Does this mean there's an error in my addmethod?

1
  • Also show the relevant rendered HTML markup. Commented Sep 27, 2013 at 14:58

1 Answer 1

2

Assuming that your input contains name="ticket",

this:

rules: {
    ticket: uniqueTicket
}

should be this:

rules: {
    ticket: {
        uniqueTicket: true
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant - so frustrating to miss it staring at your face!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.