i creating project using angularjs , want integrate validation in angularjs. requirement number should between 1-4096 in angularjs.
here 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>
you should create simple directive allow validate input in reusable, configurable , declarative way.
you have valid-number
attribute, implementation can 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 in range 1-4096.</div> </div>
Comments
Post a Comment