The Wayback Machine - https://web.archive.org/web/20240704131312/https://www.geeksforgeeks.org/how-to-reset-an-input-value-when-limit-exceeds-using-angularjs/
Open In App

How to Reset an Input Value when Limit Exceeds using AngularJS ?

Last Updated : 08 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The task is to handle an input field if the number entered by the user in the input exceeds some limit using angularJS. We define a limitTo directive and use it on an HTML input element. This directive is used for all the handling of overflowing limits. The directive calls a function on the keypress event where we can check for the limits. 

 

Example 1: This example sets the value of user input to 0 if its limit exceeds 100. 

HTML




<!DOCTYPE html>
<html ng-app="myApp">
  
<head>
    <script src=
    </script>
</head>
  
<body ng-controller="MyController" 
      style="text-align:center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <h3>
        Edit an input value when
        it exceeds the limit
    </h3><br>
      
    <!-- Custom directive limit-to -->
    <input limit-to="100"
           type="number" 
           ng-model="val"> {{val}}
             
    <script type="text/javascript">
    angular.module('myApp', []).controller('MyController',
        ['$scope', function($scope) {
              
        // Value to ng model the input
        $scope.val;
    }]).directive("limitTo",
        ['$timeout', function($timeout) {
              
        // Declaration of custom directive
        return {
            restrict: "A",
            link: function(scope, elem, attrs) {
                var limit = parseInt(attrs.limitTo);
                elem.on("keypress", function(e) {
                    $timeout(function() {
                        if(parseInt(e.target.value) > limit) {
                              
                            // Handle change here
                            scope.val = 0;
                            scope.$apply();
                            e.preventDefault();
                        }
                    });
                });
            }
        }
    }]);
    </script>
</body>
</html>


Output: As soon as the user exceeds 100, the value is set back to 0. 

 

Example 2: In this example, we change the text based on the input limit. If the value of the input is less than 0 then it goes to 0. If its value is greater than 100 then it goes to 100 otherwise it stays as it is. 

HTML




<!DOCTYPE html>
<html ng-app="myApp">
  
<head>
    <script src=
    </script>
</head>
  
<body ng-controller="MyController" 
      style="text-align: center">
    <h1 style="color: green">GeeksforGeeks</h1>
    <h3>
        Edit an input value when
        it exceeds the limit
    </h3>
      
    <!-- Custom directive limit-to -->
    <input limit-to="100" 
           type="number" 
           ng-model="val" /><br />
       {{text}}
         
    <script type="text/javascript">
    angular.module('myApp', []).controller('MyController', 
        ['$scope',function($scope) {
              
            // Value to ng model the input
            $scope.val;
            $scope.text = 'Inside Limit';
        },
    ]).directive('limitTo', 
    ['$timeout',function($timeout) {
              
            // Declaration of custom directive
            return {
                restrict: 'A',
                link: function(scope, elem, attrs) {
                    var limit = parseInt(attrs.limitTo);
                    elem.on('keypress', function(e) {
                        $timeout(function() {
                            if(parseInt(e.target.value) > limit) {
                                  
                                // Handle change here if greater
                                scope.text = 'Outside limit (greater)';
                                scope.val = 100;
                                scope.$apply();
                                e.preventDefault();
                            } else if(parseInt(e.target.value) < 0) {
                                scope.text = 'Outside limit (smaller)';
                                scope.val = 0;
                                scope.$apply();
                                e.preventDefault();
                            } else {
                                scope.text = 'Inside Limit';
                                scope.$apply();
                                e.preventDefault();
                            }
                        });
                    });
                },
            };
        },
    ]);
    </script>
</body>
</html>


Output:

 



Similar Reads

How to reset a form using jQuery with .reset() method?
In this article, we will implement the reset functionality with the reset() method in jQuery and reset all the input fields of a form. Resetting anything will set everything back to its original state. JQuery doesn't have a reset() method, but native JavaScript does. So, we convert the jQuery element to a JavaScript object. Syntax: $('element_selec
2 min read
HTML | DOM Input Reset value Property
The Input Reset value Property in HTML DOM is used to set or return the value of the value attribute of a Input reset Field. The value attribute specifies the text displayed in the reset Field. Syntax: It returns the Input Reset value Property.resetObject.valueIt is used to set the Input Reset value Property.resetObject.value = text Property Value:
2 min read
How to limit the length of a string in AngularJS ?
Validation of fields is an important process while developing a dynamic web application. In various aspects of security, the validation is quite important. Along with this, the restriction of the limit to the length of a string is also an important process that helps to include the constraint for the maximum length for an input field or a text area
5 min read
How to get form input value using AngularJS ?
Given a form element and the task is to get the form values input by the user with the help of AngularJS. Approach: We are storing the data in the JSON object after the user enters it in the input element with the help of the Angular.copy(object) method. After that, data from the JSON object can be accessed easily. Example 1: In this example, the d
2 min read
How to get input value search box and enter it in AngularJS component using Enter key ?
In this article, we will see how to get an input value entered in the search box using Enter key in AngularJS. To implement a search component in AngularJS that calls the function whenever the user presses the enter key(keyCode = 13) and then does some relatable task from the user input. This can be achieved easily using the keyup event. Here for s
3 min read
How to reset input type = "file" using JavaScript/jQuery?
In this article, we will learn how to rest input type="file" using Javascript/Jquery. We can easily reset a file using jQuery and HTML. This can also be done using JavaScript and HTML. These are the following ways: Table of Content Using the wrap method in jQueryUsing file.value = ''Using wrap method in jqueryApproach 1: Using the wrap method in jQ
2 min read
What is the Limit File Format when using &lt;input type="file"&gt; ?
In this article, we will learn how to limit file format when using HTML input tags. By using Hypertext markup language(HTML) language, we cannot restrict any users to choose any type of file from the browser (developed by the developer). But, we can apply some limitations by using &lt;input type="file"&gt;. File formats can be limited or restricted
2 min read
How to set the input field value dynamically in AngularJS ?
Given an input field and the task is to set the input field value dynamically with the help of AngularJS. Approach: A value is being set to the input field despite the user enter something or not. ng-click event is used to call a function that sets the value to 'This is GeeksForGeeks' with the help of ng-model. Example 1: In this example, the input
2 min read
HTML | DOM Input Reset Object
The Input Reset Object in HTML DOM is used to represent the HTML &lt;input&gt; element with type="reset". This tag is used to access or create the &lt;input&gt; element. This element can be accessed by using getElementById() method. Syntax: document.getElementById("Input_ID"); This Input_ID is assigned to HTML &lt;input&gt; element. Example-1: Crea
2 min read
HTML | DOM Input Reset name Property
The Input Reset name Property in HTML DOM is used to set or return the value of name attribute of a reset field. The name attribute is required for each input field. If the name attribute is not specified in an input field then the data of that field would not be sent at all. Syntax: It returns the Input reset name property.resetObject.nameIt is us
2 min read
three90RightbarBannerImg