3

I found where you can add jquery validation rules dynamically like this:

$("#ParentAdNumber").rules("add", { required: true });

But how do you attach and detach a related message to it?

3 Answers 3

7
$("#ParentAdNumber").rules("remove", "required");

From the documentation: http://docs.jquery.com/Plugins/Validation/rules

Edit: messages

Adding:

$("#ParentAdNumber").rules("add", {
    optionName: true,
    messages: {
        optionName: "message for optionName"
    }
});

Removing:

$("#ParentAdNumber").rules("remove", "messages");

Looking at the source code of Validation, it doesn't look like you can remove specific messages...

Sign up to request clarification or add additional context in comments.

4 Comments

Thx, figured that part out too, but it's the messages i'd like to add to the rules is what i can't figure out
Awesome, thanks so much for the help. Was that in the documentation?
So if I need to remove the rule I don't need to be concerned about removing the message, do I understand?
That was in the documentation. I believe you are correct: you don't need to remove the message.
4

Simple!

$("#ParentAdNumber").rules("add", {
    required: true,
    messages: {
        required: "Required input"
    }
});

you can change the message storing the rule:

var objRule = $("#ParentAdNumber").rules();

Changing:

objRule.messages.required = "New message";

and Re-applying:

$("#ParentAdNumber").rules("add", objRule);

1 Comment

Worth pointing out you have to define the message first for this to work (the first code snippet is required)
3

This should do it....

.rules("add", {required: true, messages: {required: "Required input")}

Comments

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.