简体   繁体   English

允许在on之前和之后使用十进制数和限制数<input type="“text”">

[英]Allow decimal numbers and limit numbers before and after on <input type=“text”>

I have an input type=" text" and I want to restrict the input of the users to purely numbers or numbers with decimals up to 2 decimal places.我有一个输入类型=“文本”,我想将用户的输入限制为纯数字或小数点后两位小数。

I am asking for a price input.我要求输入价格。 I want to limit the input to 17 digits before the decimal and 2 digits after the decimal point我想将输入限制为小数点前 17 位和小数点后 2 位

I wanted to avoid complex steps.我想避免复杂的步骤。 Is there a way to do it?有没有办法做到这一点?

give this a try, I'm limiting the input with maxlength and validating using a regex which you can test here: https://regexr.com/57rtg试一试,我用 maxlength 限制输入并使用您可以在这里测试的正则表达式进行验证: https://regexr.com/57rtg

<input type="text" pattern="^\d{0,17}$|(?=^.{0,20}$)^\d+\.\d{0,2}$" maxlength="20">

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

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