使输入文本只接受0-9之间的数字的模式 .
这是我的模式:
$scope.onlyNumbers = /[\u0030-\u0039]+/g;
出于某种原因,即使不在我声明的范围内,也会接受像' - '这样的字符 .
这是我的输入html:
<input style="width:40%;" type="text" name="phone" ng-minlength="10" ng-maxlength="15" ng-model="register.phone" placeholder='cell phone' ng-pattern="onlyNumbers" required title="please enter cell phone">
有人可以帮忙吗?
4 回答
为了简化
\d
=任何数值示例:http://jsfiddle.net/TheSharpieOne/JPkER/1/
您需要锚定表达式以仅将值限制为数字,并且我认为您不需要全局修饰符,因此您的表达式应该变为:
甚至更好的表达:
尝试使用大于0的数字:
$scope.onlyNumbers = /^[1-9]+[0-9]*$/
对于任何数字:`$ scope.onlyNumbers = / ^ [0-9] $ /'
您需要编辑regx模式
$ scope.onlyNumbers = / ^ [0-9] {1,7} $ /;