javascript - I want to create an input number box in AngularJS app on which a user should not be able to type decimal numbers. (an integer number input box) -



javascript - I want to create an input number box in AngularJS app on which a user should not be able to type decimal numbers. (an integer number input box) -

the thought user should not able come in decimal point while tries type in number number-input-box. thought of capturing $event on controller , alter info on input control. didn't work me. have input box in angular app web page:

<input type="number" ng-keydown="checkfordecimal($event, age)" min="1" max="125" name="inputage" ng-required="true" ng-model="model.age" />

controller:

$scope.checkfordecimal = function($event, age) { if($event.which === 110 || $event.which === 190) { //do } }

can think of improve way accomplish this?

this should directive. here's 1 illustration i've seen here (credit @my mai):

app.directive('onlydigits', function () { homecoming { require: 'ngmodel', restrict: 'a', link: function (scope, element, attr, ctrl) { function inputvalue(val) { if (val) { var digits = val.replace(/[^0-9]/g, ''); if (digits !== val) { ctrl.$setviewvalue(digits); ctrl.$render(); } homecoming parseint(digits,10); } homecoming undefined; } ctrl.$parsers.push(inputvalue); } }; });

usage:

<input type="text" name="number" only-digits>

javascript jquery html angularjs

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -