How do I disable form validation for ng-click?
Asked Answered
U

3

5

I have a form where certain inputs have "required" attribute. I also have a button that adds form fields when clicked, but it always triggers the form validation message when the required fields are not filled out. I only want form validation to trigger for ng-submit and not ng-click. Is this possible?

Unequaled answered 14/11, 2014 at 7:21 Comment(1)
use type="button" of button...!Belden
W
21

If you have a look at the W3C specification, it would seem like the obvious thing to try is to mark your button elements with type='button' when you don't want them to submit.

The thing to note in particular is where it says

A button element with no type attribute specified represents the same thing as a button element with its type attribute set to "submit"

Whizbang answered 14/11, 2014 at 7:26 Comment(1)
Ah, the problem was I didn't specify a type="" for my button and it defaulted to submit.Unequaled
K
2

when you do not want submission on specific button then define type="button". it will 100% work

Kerr answered 7/5, 2016 at 10:28 Comment(1)
Hi jitendra - this is the same solution as already suggested by Amit Sharma. Do you have any different advice?Alkali
P
1

Use novalidate for your form:

<form name="aForm" novalidate>

or use ng-form instead:

<div ng-form name="aForm">

Check this link for further info about form validation in AngularJS.

Purkey answered 14/11, 2014 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.