[英]How Can I Enter only Numbers and minus sign in this input
<div class="kinput"> <input type="text" placeholder="Enter value" maxlength="7" id="kinput" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');"> </div>
I found this expression我找到了这个表达
oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');"
but it does not let me enter the minus sign.但它不允许我输入减号。 if there is a way I can include the minus sign without allowing the other non-numeric characters..
如果有办法我可以包含减号而不允许其他非数字字符..
You can include the -
as part of the regex ( [^0-9.-]
) to ignore that character in the matching and will skip that in the replace.您可以将
-
作为正则表达式 ( [^0-9.-]
) 的一部分包含在匹配中忽略该字符并在替换中跳过该字符。
<div class="kinput"> <input type="text" placeholder="Enter value" maxlength="7" id="kinput" oninput="this.value = this.value.replace(/[^0-9.-]/g, '').replace(/(\..*)\./g, '$1');"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.