7

I have mvc attributes on fields that validate on submit. Although these attributes are on these fields, I want to override their validation if I click a SaveDraft button. Right now I try to disable validation of the field using jquery rules but the mvc attributes seem to override the rules. How can I make the MVC attributes ignore validation if I click a different button? Here is an example of what I want to do onClick but it does not work.


 $(".btnsave").click(function () {
        $('#WizForm').validate(

{ rules: { Title: false, Description: false //dont validate this field onclick }, }).Form(); });
1
  • Is this ASP.net or ASP.net MVC ? Commented Jun 29, 2011 at 16:49

3 Answers 3

14

You can use following to remove all validation rules from an element

$('#Password').rules('remove')

To remove specific rule use

$('#Password').rules('remove', 'required')

And to add rule

$("#Password").rules("add", { minlength: 2 });

Finally to check current rules

$('#Password').rules()

Note If your selector returns more than one element only the first element is used by rules method

Check out here for more

Also make sure that you have latest version. more people got in trouble with IE and jQuery-validation-1.8

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

1 Comment

may need to call $('#Password').blur() after removing the rule for the change to show up.
0

I may just not understand your issue here but couldn't you just have the btnSave not cause validation?

<asp:Button id="bntSaveDraft" runat="server" Text="Button" CausesValidation="False" />

1 Comment

I use razor mvc. But I can use the cancel class to stop the button from validating but I need to be able to add validation to fields that the user has added values to. So I need to selectively add validation and selectively disable validation. The Jquery rules would seem to do this best but it does not work.
0

(Posting just in case someone else had the same issue as I did with MVC3 Unobtrusive validation library)

Beygi answer worked for me, but I made a mistake in declaring my elements.

I declared my elements with only and ID tag, and not with the NAME tag. This does not work with MVC3 Unobtrusive validation library, as it requires the NAME tag.

<input id="firstName" value="" data-val="true" data-val-required="required" />

Needed to be

<input id="firstName" **name="firstName"** value="" data-val="true" data-val-required="required" />

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.