简体   繁体   中英

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.

 <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>

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