How can I show the error if the user write decimal point(.) in input field type number. I am using ng-pattern but it didn't validate when single full stop is entered.
如果用户在输入字段类型编号中写入小数点(。),如何显示错误。我正在使用ng-pattern但是在输入单个完整停止时它没有验证。
For ex. If i entered number"9988587146." it didn't shows error or call ng-pattern function if i write two dots then it shows error . for Ex "9988587146.."Everthing working fine all validationS except single dot (.) in input field HTML
对于前者如果我输入了数字“9988587146”。它没有显示错误或调用ng-pattern函数,如果我写两个点然后它显示错误。对于Ex“9988587146 ..”Everthing工作正常所有验证,但输入字段HTML中的单点(。)除外
<input type="number" name="number" class="form-control" placeholder="Phone Number" ng-maxlength="10" ng-minlength="10" limit-to="10" step="0" ng-pattern="checkPattern()" ng-model="user.number" required/>
My check Pattern function .
我的检查模式功能。
var isnum = /^[0-9]{10,10}$/.test($scope.user.number);
return isnum;
1 个解决方案
#1
2
There are two things you need to change in your code:
您需要在代码中更改两件事:
-
ngPattern
does not accept a function as its value. Only regex or a string that evaluates to a regex are valid values. - Like @Wiktor Stribiżew suggested in the comments, input with type
number
dose not work with pattern validation. Onlytext
,search
,tel
,url
,email
orpassword
will let you validate using a pattern. Since you try to validate a phone number anyway, I suggest you use typetel
.
ngPattern不接受函数作为其值。只有正则表达式或评估为正则表达式的字符串才是有效值。
与@WiktorStribiżew在评论中建议的那样,输入类型编号不适用于模式验证。只有文本,搜索,电话,网址,电子邮件或密码才能让您使用模式进行验证。既然您尝试验证电话号码,我建议您使用tel类型。
In your template, use:
在您的模板中,使用:
<input type="tel" ng-pattern="checkPattern" name="number" class="form-control" placeholder="Phone Number" ng-maxlength="10" ng-minlength="10" limit-to="10" step="0" ng-model="user.number" required/>
And in the controller:
在控制器中:
$scope.checkPattern = /^[0-9]{10,10}$/;
#1
2
There are two things you need to change in your code:
您需要在代码中更改两件事:
-
ngPattern
does not accept a function as its value. Only regex or a string that evaluates to a regex are valid values. - Like @Wiktor Stribiżew suggested in the comments, input with type
number
dose not work with pattern validation. Onlytext
,search
,tel
,url
,email
orpassword
will let you validate using a pattern. Since you try to validate a phone number anyway, I suggest you use typetel
.
ngPattern不接受函数作为其值。只有正则表达式或评估为正则表达式的字符串才是有效值。
与@WiktorStribiżew在评论中建议的那样,输入类型编号不适用于模式验证。只有文本,搜索,电话,网址,电子邮件或密码才能让您使用模式进行验证。既然您尝试验证电话号码,我建议您使用tel类型。
In your template, use:
在您的模板中,使用:
<input type="tel" ng-pattern="checkPattern" name="number" class="form-control" placeholder="Phone Number" ng-maxlength="10" ng-minlength="10" limit-to="10" step="0" ng-model="user.number" required/>
And in the controller:
在控制器中:
$scope.checkPattern = /^[0-9]{10,10}$/;