简体   繁体   中英

How to restrict “type=number” to certain decimal places

I am developing an application in which i require to restrict the amount "type=number" filed to certain (2 places) places. Below is the HTML rules for element : Please can someone help me regarding this query, Thanks in advance :

  <input type="number" name="amount" ng-model="orderStock.amount" class="form-control" id="amount" step="0.01" min="10" max="9999.99" placeholder=" Please enter Amount" ng-disabled="isDisabled" required /> 

You can attach an input event listener to your input element and check if it's value contains a comma. If it does, check if there are more than two characters to the right of the comma and remove the excess.

 function process() { var text = document.getElementById("amount").value; var index = text.indexOf("."); if (index > -1) { if (text.length - index > 3) { document.getElementById("amount").value = text.substr(0, text.length - 1); } } } document.getElementById("amount").addEventListener("input", process); 
 <input type="number" name="amount" ng-model="orderStock.amount" class="form-control" id="amount" step="0.01" min="10" max="9999.99" placeholder=" Please enter Amount" ng-disabled="isDisabled" required /> 

If you want to have all the code inside the html tag, you can do this:

 <input type="number" name="amount" ng-model="orderStock.amount" class="form-control" id="amount" step="0.01" min="10" max="9999.99" oninput="var text = this.value;var index = text.indexOf('.');if (index > -1) {if (text.length - index > 3) {this.value = text.substr(0, text.length - 1);}}" placeholder=" Please enter Amount" ng-disabled="isDisabled" required /> 

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