[英]How to keep + and - in ng-pattern using Angular.js
I need one help. 我需要一个帮助。 I have one text field which should not take the alpha numerical value but take
numbers,+,-,.
我有一个文本字段,不应使用字母数字值,而应使用
numbers,+,-,.
using Angular.js . 使用Angular.js。 I am explaining my code below.
我在下面解释我的代码。
<div ng-class="{ 'myError': billdata.lati.$touched && billdata.lati.$invalid }">
<input type="text" name="lati" id="latitude" class="form-control oditek-form" placeholder="Add Latitude coordinate" ng-model="latitude" ng-keypress="clearField('businessno');" ng-pattern="/^[0-9]+([,.][0-9]+)?$/">
</div>
But here problem is while this field has value like -122.32379000000003
,its showing the error message.here i need this field only can accept numbers,+,- and .
但是这里的问题是当该字段的值像
-122.32379000000003
,它显示错误消息。在这里,我需要此字段只能接受numbers,+,- and .
white space are also not allowed with alphanumerical value. 空格也不允许使用字母数字值。 Please help me.
请帮我。
使用/^[.+-]?[0-9]+([,.][0-9]+)?$/
To meet the requirement , you can use the below pattern 为了满足要求,可以使用以下模式
ng-pattern="/^[0-9]{1,7}$/"
Example: 例:
<div ng-class="{ 'myError': billdata.lati.$touched && billdata.lati.$invalid }">
<input type="text" name="lati" id="latitude" class="form-control oditek-form" placeholder="Add Latitude coordinate" ng-model="latitude" ng-keypress="clearField('businessno');" ng-pattern="/^[0-9]{1,7}$/">
</div>
Hope this helps you. 希望这对您有所帮助。
Change you ng-pattern regex to ng-pattern="/^([\\-\\+0-9]{0,1})(\\.?[0-9])*$/"
将您的ng-pattern正则表达式更改为
ng-pattern="/^([\\-\\+0-9]{0,1})(\\.?[0-9])*$/"
where ^([\\-\\+0-9]{0,1})
which allows +,- or numbers as a first character 其中
^([\\-\\+0-9]{0,1})
允许以+,-或数字作为第一个字符
<form name="myForm">
<div ng-class="{ 'myError': billdata.lati.$touched && billdata.lati.$invalid }">
<input type="text" ng-pattern="/^([\-\+0-9]{0,1})(\.?[0-9])*$/" name="lati" id="latitude" class="form-control oditek-form" placeholder="Add Latitude coordinate" ng-model="latitude" ng-keypress="clearField('businessno');" >
</div>
<label ng-show="myForm.lati.$error.pattern">Error</label>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.