I have this following html code. I donot want the decimal point to be there in this input field.
can someone let me know how to achieve this.
<input type="number" min="0" step="1" ng-pattern="/^(0|[1-9][0-9]*)$/" ng-model="params.data" />
I have this following html code. I donot want the decimal point to be there in this input field.
can someone let me know how to achieve this.
<input type="number" min="0" step="1" ng-pattern="/^(0|[1-9][0-9]*)$/" ng-model="params.data" />
Please, keep in mind that ngPattern
is just used to validate the field in a form (formName.field.$error.pattern
), it still allows you to type a decimal point or whatever even if your regex doesn't match.
You can do it using:
I made this snippet showing both ways:
(function() {
"use strict";
angular
.module('app', ['ngMask'])
.controller('mainCtrl', mainCtrl);
function mainCtrl($scope) {
$scope.params = {};
$scope.checkNumber = function() {
if ($scope.params.data) {
$scope.params.data = $scope.params.data.replace(/\D+/, '');
}
}
}
})();
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ngMask/3.1.1/ngMask.min.js"></script>
</head>
<body ng-controller="mainCtrl">
<form name="form" novalidate>
<label for="withoutMask">Input without mask: </label>
<input type="text" id="withoutMask" ng-model="params.data" ng-change="checkNumber()">
<hr>
<label for="mask">Input with mask: </label>
<input type="text" id="mask" mask="d" repeat="15" restrict="reject" limit="false" ng-model="otherInput">
</form>
</body>
</html>
I hope it helps!