AngularJS | ng-required Directive

The ng-required Directive in AngularJS is used to specify the required attribute of an HTML element. The input field in the form is required only if the expression inside ng-required directive returns true. It is Supported by <input>, <select> and <textarea>.

Syntax:

<element ng-required="expression"> Contents... </element> 

Example 1: This example uses ng-required Directive to set input text field of form tag to required.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>ng-required Directive</title>
      
    <script src=
    </script>
  
    <style>
        .req {
            font-size: 90%;
            font-style: italic;
            color: red;
        }
    </style>
</head>
  
<body style="text-align:center">
      
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>ng-required Directive</h2>
      
    <div ng-app="app" ng-controller="myCtrl">                 
        <form name="myForm">
            <p>Enter Your Name: <br/>
                <span>
                    <input type="text" name="name"
                    ng-model="Name" ng-required ="true" />
                </span>
            </p>
              
            <span ng-show="myForm.name.$invalid" class="req">
                This is the required field.
            </span>
        </form>
    </div>
      
    <script>
        var app = angular.module("app", []);         
        app.controller("myCtrl", function($scope) { 
            $scope.Name = "";
        });
    </script>
</body>
  
</html>                    

chevron_right


Output:
Before Input the name:
ngrequired
After Input the name:
ngrequired

Example 2: This example uses ng-required Directive to create required field after checked the checkbox.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>ng-required Directive</title>
      
    <script src=
    </script>
</head>
  
<body ng-app="app"style="text-align:center">
      
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>ng-required Directive</h2>                     
      
    <div ng-controller="geek" style="font-family:arial;"
        <form name="myform">
            <label for="requiredValue">
                Is Required:
            </label>
              
            <input type="checkbox" ng-model="requiredValue"
                        id="required" />
            <br><br>
              
            <label for="input">Enter Name: </label>
              
            <input type="text" ng-model="model" id="input"
                name="input" ng-required="requiredValue" />
            <br>         
              
            <p style="color:red;"
                    ng-show='myform.input.$error.required'>
                Name is required
            </p
        </form
    </div>     
      
    <script>     
        var app = angular.module('app', []) 
        app.controller('geek', ['$scope', function($scope) { 
            $scope.requiredValue = true; 
        }]); 
    </script
</body>
  
</html>                    

chevron_right


Output:
Before checked the checkbox:
ngrequired
After checked the checkbox:
ngrequired



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.