Search code examples
htmlangularjsvalidationng-pattern

How to prevent AngularJS from validating form controls for the first time


I my form I have some input controls which are bounded with controller's scope data. Based on users selection I am bounding selected item by using ng-model to input controls.

I am validating those inputs by using ng-maxlength ,minlength ,ng-pattern and other inbuilt validation directives.

Class for highlighting the invalid values.

.ng-invalid { border-color:red; }

But when user wants to add a new product, then I am creating an empty object and adding it to controller's scope data.

At the very first time while creating new item I don't want to highlight every thing with red, because very thing is empty.

Is there any way by which I can highlight invalid input on their focus and after it will show as invalid until use put some valid values in it.

When use select any existing data then I am validating control at that movement.


Solution

  • You can use forms' $pristine as a condition for your classes.