1

I am creating a project using AngularJS and I want to integrate validation in AngularJS. My requirement is that the number should be between the 1-4096 in AngularJS.

Here is my code:

<div class="col-lg-6 col-md-6">
    <input type="text"  class="form-control" placeholder="VLAN ID" ng-model="exchange.vlanId" valid-number/>
</div>
3
  • 3
    Use simple comparison: num >= 1 && num <= 4096 on blur/keyup event Commented Jan 29, 2016 at 7:17
  • 1
    Adding onto the answer @Tushar gave, check out the documentation. There are provisions for min and max, which if crossed trigger an error. Commented Jan 29, 2016 at 7:24
  • valid-number Have you created a directive for it @Karan. Commented Jan 29, 2016 at 7:34

2 Answers 2

4

You should create very simple directive that would allow to validate input in reusable, configurable and declarative way.

You already have valid-number attribute, so the implementation can look like:

angular.module('demo', []).directive('validNumber', [function() {
    return {
        require: 'ngModel',
        link: function(scope, elem, attrs, ctrl) {
            if (!ctrl) return;
            var range = attrs.validNumber.split(',').map(Number);
            ctrl.$validators.validNumber = function(value) {
                return value >= range[0] && value <= range[1];
            };
        }
    };
}]);
.error {color: brown;}
<script src="https://code.angularjs.org/1.4.8/angular.min.js"></script>

<div ng-app="demo">
    
    <form name="form">
        <input type="text" class="form-control" placeholder="VLAN ID" name="vlanId"
               ng-model="exchange.vlanId" valid-number="1,4096" />
    </form>
    
    <div class="error" ng-show="form.$dirty && form.vlanId.$error.validNumber">VLAN ID should be in range 1-4096.</div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

You can bind an event on the input and call a function with passing the model in it:

<input type="text"  class="form-control" placeholder="VLAN ID" 
       ng-model="exchange.vlanId"
       ng-keydown="obj.validate(exchange.vlanId)" valid-number/>

Now in the controller you can define a method:

yourApp.controller('theController', ['$scope', function($scope){
    $scope.obj = {
        validate:function(val){
           if(val < 1 || val > 4096){
              alert(val+' is out of range');
           }
        }
    };
}]);

And the directive valid-number can also be used:

yourApp.directive('validNumber', function($scope){
    return {
       restrict:'E',
       link:function(scope, el, attrs){
          el.on('keydown', function(){
             el.css('border', function(){
               return  scope.exchange.vlanId < 1 || scope.exchange.vlanId > 4096
               ? "red" : "green";
             });
          });
       }
    };
});

3 Comments

Did you notice valid-number attribute?
The valid-number directive.
i see, that could also be used. hmmm... it was better to ask for it...:(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.