I need to implement an input filter for limiting numeral entry in the format 1234.35. That is, maximum four before . and two decimal places. I am using this regular expression pattern:
Pattern.compile("[0-9]{0,4}+((\\.[0-9]{0,2})?)||(\\.)?");
This works, but once I enter a number in the edit text and try to edit the values before the decimal places, I can't edit them. I can only delete them.
What is wrong?
Based upon what you said and I think it looks like you were trying to do, I would use this regular expression:
^(\d{0,4})(\.\d{1,2})?$
It matches '0-4 digits' with or without 'a decimal point and two numbers' following them. If there is a decimal point, then either one or two digits must follow it. For instance: 5, 1234, 1234.56, .2, and .31 are all valid and matched by the expression, but .123, 1234., 1234.567, 12345, and . are NOT matched.
Alternatively, to allow numbers ending in decimals (like ., 1234., and the like), use this modification:
^(\d{0,4})(\.(\d{1,2})?)?$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With