just ram

stuff I should remember

jQuery Validation Plugin – Dynamically change validator message

**

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.

Comments