简体   繁体   中英

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.

<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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM