1

I have tried using below code for which the textarea accepts numbers greater than 0 and less than 5, everything is working fine but when comes to decimal it unable to validate. below is my code

 <body ng-controller="MainCtrl">
  <form name="myform">
  <input type="number" min="0" name="minmax" ng-model="number" required="required" maxlength="10" numbers-only/>
 <span ng-show="myform.$error.numbersOnly">Enter the numbers only between 0 to 5 </span>
 </form>
 </body>

and here is controller

 var app = angular.module('plunker', []);
 app.directive('numbersOnly', function(){
   return {
     require: 'ngModel',
     link: function(scope, element, attrs, modelCtrl) {
       modelCtrl.$parsers.push(function (inputValue) {


       if(parseInt(inputValue) <= 5  && parseInt(inputValue) > 0){
         modelCtrl.$setValidity('numbersOnly', true);
         return inputValue;
       } else {
         modelCtrl.$setValidity('numbersOnly', false);
          return modelCtrl.$modelValue;
       }

    });
   }
 };
}); 

function MyCtrl($scope) {
$scope.number = ''
 }

and here is my working plunker http://plnkr.co/edit/rOShiJYCZrXcgMdr951i?p=preview

2 Answers2

0

Kindly read this below discussion, before you workaround this issue

Is there a float input type in HTML(5)?

Now you need to Set step="0.01 on your input number field. it will be can take decimal and integer values.

<input type="number" name="minmax" ng-model="number" required="required" step="0.01 maxlength="10" numbers-only/>

Plunker

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
-2

Try using parseFloat

  if(parseFloat(inputValue) <= 5  && parseFloat(inputValue) > 0){

and change input type="number" to input type="text"

sisyphus
  • 452
  • 5
  • 13