[英]`ng-pattern` - how to check for only numbers?
I am using an ng-pattern
to check for ONLY numbers in my input.我正在使用
ng-pattern
检查输入中的唯一数字。 But for me that's not working.但对我来说这行不通。 I can able to type in
characters
after the number
.我可以在
number
后输入characters
。 How can I restrict that?我怎么能限制呢?
here is my code :这是我的代码:
<div ng-app ng-controller="formCtrl">
<form name="myForm" ng-submit="onSubmit()">
<input type="text" ng-model="price" name="price_field" ng-pattern="/^[0-9]/" required>
<span ng-show="myForm.price_field.$error.pattern">Not a valid number!</span>
<input type="submit" value="submit"/>
</form>
</div>
Use this expression使用这个表达
ng-pattern="/^[0-9]*$/"
UPDATE :更新 :
for accepting the only characters,you can use like this.为了接受唯一的字符,你可以这样使用。
ng-pattern="/^[a-zA-Z]*$/"
试试这个正则表达式:它应该可以工作/^[0-9]+$/
+ 符号用于接受一位或多位数字。
ng-pattern="/^[0-9]*$/"
//change to ng-pattern="/^[0-9]*$/"
//改为
ng-pattern="/^\d{1,10}$/"
NOTE: '\\d' can check not only 0-9, also for many digits like greek digits注意:'\\d' 不仅可以检查 0-9,还可以检查许多数字,如希腊数字
For reactive form对于反应形式
this.form = fb.group({
number: ['', [Validators.required, Validators.pattern("^[0-9]*$")]]
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.