Open In App
Related Articles

What’s the difference between ng-pristine and ng-dirty in AngularJS ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

AngularJS supports client-side form validation. AngularJS keeps track of all the form and input fields and it also stores the information about whether anyone has touched or modified the field or not. The two different classes ng-dirty and ng-pristine that are used for form validation, are described below:

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.

Example 1: This example describes the usage of the ng-pristine and ng-dirty classes in AngularJS by implementing it using the <input> element.

HTML

<!DOCTYPE html>
<html>
  
<head>
    <title>
        Difference between ng-pristine and ng-dirty
    </title>
    <script src=
    </script>
    <style>
        body {
  
            font-family: Arial;
            text-align: center;
        }
  
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1 style="color:green">GeeksforGeeks</h1>
    <h3>ng-pristine and ng-dirty in AngularJS</h3>
    <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>
</body>
</html>

                    

Output:

 

Example 2: This example describes the usage of the ng-pristine and ng-dirty classes in AngularJS by implementing it using the <textarea> element.

HTML

<!DOCTYPE html>
<html>
  
<head>
    <title>
        Difference between ng-pristine and ng-dirty
    </title>
    <script src=
    </script>
    <style>
        body {
            text-align: center;
            font-family: Arial;
        }
  
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h3> ng-pristine and ng-dirty in AngularJS</h3>
    <form ng-app="" name="myForm">
        <textarea name="text" ng-model="data.text"></textarea>
        <p class="info" ng-show="myForm.text.$pristine">
            Now Pristine.
        </p>
        <p class="error" ng-show="myForm.text.$dirty">
            Now Dirty
        </p>
    </form>
</body>
</html>

                    

Output:

 

Difference between ng-pristine and ng-dirty: The main difference between them is that the ng-dirty is used to tell that the input field is modified by the user, whereas the ng-pristine is used to tell us that the field is untouched by the user. 



Last Updated : 08 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads