输入类型编号 - 无法删除小数点

时间:2016-07-22 21:01:17

标签: javascript html angularjs html5

我有以下html代码。我不希望在这个输入字段中有小数点。

有人可以告诉我如何实现这一目标。

 <input type="number" min="0" step="1" ng-pattern="/^(0|[1-9][0-9]*)$/" ng-model="params.data" />

1 个答案:

答案 0 :(得分:1)

请注意,ngPattern仅用于验证表单(formName.field.$error.pattern)中的字段,它仍允许您输入小数点或其他任何内容,即使您的正则表达式没有&#39} ; t匹配。

您可以使用以下方式执行此操作:

  1. ngChange 指令;
  2. ngMask 模块(总是我需要处理带有我使用它的面具的输入)。
  3. 我让这个代码段显示了两种方式:

    &#13;
    &#13;
    (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+/, '');
           }
         }
      }
    })();
    &#13;
    <!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>
    &#13;
    &#13;
    &#13;

    我希望它有所帮助!