Skip to content
Related Articles

Related Articles

What’s the difference between ng-pristine and ng-dirty in AngularJS?
  • Last Updated : 31 Dec, 2019

AngularJS supports client-side form validation. AngularJS keeps tracks of all the form and input field and it also stores the information about whether anyone has touched or modified the field or not.

Let’s See the two different class ng-dirty and ng-pristine that are used for form validation

  • ng-pristine: The ng-pristine class tells that the form has not been modified by the user. This returns true if the form has not been modified by the user.
    Return type:
    Return Boolean True if the form/input field is not modified by the user else it returns False.
  • ng-dirty: The ng-dirty class tells that the form has been made dirty (modified ) by the user. It returns true if the user has modified the form.
    Return type:
    Return Boolean True if the form/input field is modified by the user else it returns False.

Difference between ng-pristine and ng-dirty:
The main difference between both of them is that ng-dirty is used to tell that the input field is modified by the user and the ng-pristine is used to tell us that the field is untouched by the user.
Let’s see with the help of a small example to clear out everything.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
      Difference between ng-pristine and ng-dirty
  </title>
</head>
  
<body>
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
        <script src=
      </script>
        <form ng-app="" name="myForm">
            <input name="email" 
                   ng-model="data.email">
            <div class="info" 
                 ng-show="myForm.email.$pristine">
                Now Pristine.
            </div>
            <div class="error" 
                 ng-show="myForm.email.$dirty">
                Now Dirty
            </div>
        </form>
    </center>
</body>
  
</html>

chevron_right


Output:
Before:

After:

My Personal Notes arrow_drop_up
Recommended Articles
Page :