首页 > 解决方案 > AngularJS:ng-pattern 在输入字段中阻止非数字用户输入无效

问题描述

我试图防止输入字段中的非数字用户输入:

 <input ng-model-options="{allowInvalid: false}" ng-pattern="/^[0-9]*$/"
        class="cell large-3"  ng-model="val1" />

不幸的是,我也可以在输入中输入字母。我怎样才能改变这种行为?

标签: javascriptangularjsng-pattern

解决方案


我试图防止输入字段中的非数字用户输入

使用type="number"

<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app>
    <input type="number" ng-model="value" /><br>
    value={{value}}
</body>

有关详细信息,请参阅


推荐阅读