[英]Can not Allow the decimal value inside input field using Angular.js
I need one help.I have a input field validation and this field only accepts the number including decimal value also using Angular.js pattern.I am explaining my code below. 我需要一个帮助。我有一个输入字段验证,并且此字段也使用Angular.js模式接受仅包含十进制值的数字。我在下面解释我的代码。
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">
Unit Sale Price :
</span>
<div ng-class="{ 'myError': billdata.usp.$touched && billdata.usp.$invalid }">
<input type="text" name="usp" id="usp" class="form-control"
placeholder="Add Unit Sale Price"
ng-model="unit_sale_price"
ng-keypress="clearField('usp');"
ng-keyup="setLatestSalePrice();"
ng-pattern="/^(0|[1-9][0-9]*)$/">
<div style="clear:both;"></div>
</div>
</div>
<div class="help-block" ng-messages="billdata.usp.$error"
ng-if="billdata.usp.$touched || billdata.usp.$error.pattern">
<p ng-message="pattern" style="color:#F00;">
This field needs only number(e.g-0,1..9).
</p>
</div>
Here my problem is the decimal like value are also not taken by this field.Here i need only characters are also not allowed.Please help me. 在这里我的问题是该字段也不接受小数点后的值。在这里我只需要字符也不允许。请帮助我。
您可以使用input [type =“ number”]来解决此问题:
<input type="number" name="usp" id="usp" class="form-control" placeholder="Add Unit Sale Price" ng-model="unit_sale_price" ng-keypress="clearField('usp');" ng-keyup="setLatestSalePrice();">
Use can also use the regex for this : 使用还可以为此使用正则表达式:
<input type="text" name="usp" id="usp" class="form-control" placeholder="Add Unit Sale Price" ng-model="unit_sale_price" ng-keypress="clearField('usp');" ng-keyup="setLatestSalePrice();" ng-pattern = "^[0-9]+([.][0-9]+)?$">
And use the below expression for error 并使用以下表达式表示错误
ng-show="regForm.password.$error.pattern"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.