just in ram

A list of stuff I should remember but never do

Archive for the ‘jQuery Validation Plugin’ tag

jQuery Validation Plugin – Dynamically change validator message

with 4 comments


UPDATE:
Please read Guu’s comment below for a far better solution to this problem!

I’ve been using the jQuery Validation Plugin and needed to change a validation message dynamically based on the user input. I had a quick search but couldn’t find an easy solution. Unfortunately the messages are not changeable after the initial construction of the validator object. I managed to get around the problem by removing the validator an then re-adding with a new message:

Create validator

$("#Enquiry").validate({
    rules: {
        EnquiryText: { required: true }
    },
    messages: {
        EnquiryText: "Enquiry must contain an entry."
    }
});

Dynamically change message

function setEnquiryValidationTo(message) {
    $("#EnquiryText").rules("remove");
    $("#EnquiryText").rules("add", {
        required: true,
        messages: {
            required: message
        }
    });
}

There may be a better/easier way to do this but I couldn’t find one via Google.

Written by Justin Ramel

June 25th, 2009 at 9:43 pm

Posted in jQuery

Tagged with