I am quite new to regex and I have a problem with regex expression.
I want to only allow a user to enter positive numbers, with or without one dot and up to 2 decimals (can be only 1 decimal also). Upon user typing the text inside the textbox, and if they type the wrong format, I want to remove the other characters and replace the value with the correct format.
Valid examples:
123.12
2
56754
92929292929292.12
0.21
3.1
.90
Invalid examples:
12.1232
2.23332
e666.76
-1.23
-54.3242
3.98A
56B
BBB.12C
14.23.56
1..45
Currently I found one solution using the following regex :
$("#SomeElement").keyup(function () {
this.value = this.value.replace(/(\.\d\d)\d+|([\d.]*)[^\d.]/, '$1$2')
});
There are two problems with that
- It allows me to enter multiple dots. (e.g 123.89.80)
- Even though if I type like letter "a" once, it filters but if I hold down the letter "a" in keyboard, it still allows to enter (e.g. AAAAAAAAA12), maybe is it because of "keyup" event?
Thank you in advance.