【问题标题】:Angular JS directive for number greater than 0大于 0 的数字的 Angular JS 指令
【发布时间】:2017-03-14 20:17:24
【问题描述】:

我有以下输入 <input type="text" name="area" class="form-control" ng-pattern="onlyNumbers" ng-model="myForm.area" required> 和 ng-pattern 只允许输入有数字。

$scope.onlyNumbers = /^\d+$/;

我还想用某种方式告诉输入数字必须大于 0。这意味着用户不能输入像 023 这样的东西,它必须是 23。

【问题讨论】:

    标签: javascript jquery regex html angularjs


    【解决方案1】:

    你可以改变正则表达式:

    $scope.onlyNumbers = /^[1-9][0-9]*$/;
    

    【讨论】:

      【解决方案2】:

      这将允许任何大于零的数字(例如,0.0002)。

      $scope.onlyNumbers = /^0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$/

      【讨论】:

      • 它允许输入负值,例如:-0.1
      【解决方案3】:

      允许带有,. 分隔符的浮点数的选项。

      $scope.onlyNumbers = /^(0*[1-9][0-9]*([\.\,][0-9]+)?|0+[\.\,][0-9]*[1-9][0-9]*)$/;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-01
        • 1970-01-01
        • 2017-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-01
        • 2016-12-02
        相关资源
        最近更新 更多