简体   繁体   中英

Javascript Regex to digit/decimal and minus sign

In my web form textbox, I want to allow only numerical value with only 2 digit after decimal and there can negative (minus) sign before the digit. For example xxx.xx or -xxx.xx OR xxx OR -xxx . I know following code will do almost my requirement but how should I allow only -(minus) sign before digit?

onBlur="this.value = this.value.replace(/[^0-9\.]/g, '');"

So whatever user types, alpha characters and other symbols should be removed.

this.value.replace(/^(-?).*?(\d+\.\d{2}).*$/, "$1$2");

删除其他字符后,请删除不是第一个字符的所有减号:

onBlur = "this.value = this.value.replace(/[^0-9\.-]/g, '').replace(/(.)-+/g, '$1');"

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