简体   繁体   English

如何在输入=“文本”中限制十进制数字仅显示整数

[英]How to restrict decimal numbers in input=“text” show only integer numbers

How to restrict decimal numbers from a <input="text"> show only integer numbers. 如何限制<input="text">十进制数字仅显示整数。 The field is only show like 11 not 11.00 该字段仅显示为11而不是11.00

<input type="text">

Without using 不使用

<input type="number">

You can use parseInt() to remove decimal part without rounding ( 11.9 => 11 , 11.1 => 11 and etc): 可以使用parseInt() 没有四舍五入 (以去除小数部分11.9 => 1111.1 => 11和等):

<input type="text" id="num">

var n = 11.13;
document.getElementById('num').value = parseInt(n); // 11

or just + operator: 或者只是+运算符:

var n = 11.13;
document.getElementById('num').value = +n; // 11

You can use match() method to checking format of input value. 您可以使用match()方法检查输入值的格式。

 var input = document.getElementById("number"); var lastValue = ""; input.addEventListener("keydown", valueCheck); input.addEventListener("keyup", valueCheck); function valueCheck(){ if (input.value.match(/^[0-9]*$/)) lastValue = input.value; else input.value = lastValue; } 
 <label>Type your value</label> <input type="text" id="number" /> 

Maybe you can add a min attribute to your input html element. 也许您可以向您的输入html元素添加一个min属性。 like this 像这样

<input type="number" min="0">
<input type="text" pattern="([d]+)">

You can use modern HTML5 attribute pattern for input validation on client side. 您可以使用现代HTML5属性模式在客户端进行输入验证。 Input with decimal value would be invalid. 用十进制值输入将无效。

https://jsfiddle.net/rry32tm1/ https://jsfiddle.net/rry32tm1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 maskRe 限制 ExtJs Textfield 只接受数字。 (应接受 Positive 、 Negative 、 integer 和 decimal 数字。) - How to restrict ExtJs Textfield to accept only numbers using maskRe . (Should accept Positive , Negative , integer and decimal numbers.) 淘汰赛-将输入限制为仅带小数点的数字 - Knockout - restrict input to only numbers with one decimal point Javascript / Jquery将输入字段限制为一个小数和数字 - Javascript/Jquery to restrict input field to one decimal and numbers only 如果文本输入是整数,如何在 Javascript 中只允许输入两个数字? - If text input is an integer, how to only allow two numbers in Javascript? 对话框将输入限制为仅数字 - Dialogbox restrict input to numbers only 检查文本是否只是十进制数字? - Checking text is only decimal numbers or not? 如何将输入限制为仅接受数字? - How do I restrict an input to only accept numbers? 如何限制输入文本框只能以 Angular2 形式输入数字? - How to restrict input textbox to enter numbers only in Angular2 form? 将文本字段限制为仅数字的最佳方法? - Best way to restrict a text field to numbers only? 仅接受使用Javascript输入的带有小数的数字? - Only Accept Numbers with Decimal in a Input Using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM