javascript - Angular material input number change format -
in angular-material app have field validation:
<input type="number" ng-model="myvalue" name="myvalue" min="0" max="100" ng-pattern="/^[0-9]\d*(\.\d+)?$/" required>
it works fine. need make allow user type .75 , instead of 0.75
so question how apply filter:
if( myvalue.substring(0,1) == "." ){ myvalue = "0" + myvalue; }
before angular material ng-pattern
, type="number"
validation
you can write directive $filter
, $formatters
, $parsers
:
app.directive('parser', ['$filter', function($filter) { return { restrict:'a', require: 'ngmodel', link: function(scope, element, attrs, ctrl) { ctrl.$formatters.unshift(function (a) { return $filter('myfilter')(ctrl.$modelvalue) }); ctrl.$parsers.unshift(function (viewvalue) { element[0].value = $filter('myfilter')(element[0].value); return element[0].value; }); } }; } ]); app.filter('myfilter', [ function() { return function(input) { if( input && input.substring(0,1) == "." ){ input = "0" + input; } return input; }; } ]);
however cannot use type number
because remember .
not supported , doesn't parses integer
hope give direction
Comments
Post a Comment