简体   繁体   English

如何在此输入中仅输入数字和减号

[英]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.

相关问题 只允许数字和减号 - Allow numbers and minus sign only 如何在jquery中添加减号? - How can I add minus sign in jquery? 如何覆盖angularjs的默认值,以在负数周围使用括号(())而不是减号(-)? - How can I overwrite the default of angularjs to use parenthesis (()) around negative numbers instead of the minus (-) sign? 如何使用Jquery验证输入文本框中的允许减号(-)和仅数字? - How to validate allow minus(-) and only numbers in input textbox using Jquery? 文本输入:将输入限制为数字 (0-9) 和减号 (-)。 没有按预期工作 - Text Input: Limit input to numbers (0-9) and the minus sign (-). Not working as expected 如何使用输入type = text字段仅接受数字,减号和小数,而Angular仅接受 - How to use an input type=text field to accept only digits, minus sign and decimals and nothing more with Angular 如何限制输入文本框只能以 Angular2 形式输入数字? - How to restrict input textbox to enter numbers only in Angular2 form? 如何使用Enter键将项目添加到输入中,并且仅在单击按钮时才提交表单? - How can I add items to an input using the enter key and only submit the form when i click a button? 如何进行“输入”,只允许输入元音字母? Javascript - How can I make an "input" that allows you to enter only vowel letters? Javascript 如何只从输入中获取数字 - how can get only numbers from the input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM