Currently I'm using a filter (for formatting currency), however, the user is still able to enter leading zeros:
00000334.43 // Is accepted but shouldn't be..
03000334.43 // I don't want a leading zero unless followed by a .
I want to only accept a leading zero if it is the zero before a .
.
I understand that with a blank editText
, this would be impossible, so I would like to remove the zero unless the user types in a .
afterwards, in that use case, e.g:
User types 0 // 0 - Ok
User types . // 0. - This is fine
0 // 0 - Ok
4 // 04 - Not ok, 0 should be removed from the text edit.
I am doing this inside a custom class which extends the DigitsKeyListener
class - using the latter's filter()
method:
public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
int dstart, int dend)
{
// How can I achieve this inside the filter method?
}
What I've tried so far:
replaceFirst()
- I've attempted to use this regex - .replaceFirst("^0+(?!$)", "")
however it causes problems when trying to insert digits at the start after typing. I am also unsure how I can actually use this properly inside the filter method.
Any help is appreciated.