0

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.

user2381114
  • 471
  • 2
  • 6
  • 16
  • 1
    You're doing this analysis on a key-by-key basis? If only zeros have been entered so far, can't you just postpone this processing until either a non-zero or a decimal is entered? – aliteralmind Feb 23 '14 at 00:18
  • 1
    @aliteralmind Indeed, as I need to check whether the digit next to it is a decimal mark, I can't think of a better way? It's because the rest of my filtering is also being done on a key-by-key basis also – user2381114 Feb 23 '14 at 00:24

2 Answers2

0

looks like this regex in the .replaceFirst should work: ^0+(?=\d)

Try it out, and see if that helps.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
  • Thank you for this, but do you know how I could use it inside of InputFilter's filter() method? I was thinking of using charAt(i + 1) to check for whether the zero is next to a . but I'm unsure – user2381114 Feb 23 '14 at 21:58
  • `InputFilter` - is that your own class? Can you describe? – Alvin Bunk Feb 23 '14 at 22:17
  • Sorry @user2945993; I originally didn't see this was Android you were working on. I was thinking that you could use something like Java's String `replaceAll(String regex, String replacement)` or something similar. Link to that in Java is here: [link](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)) – Alvin Bunk Feb 25 '14 at 03:44
0

Make input filter for this:

const val separator = "."
private val filterAmountValue: InputFilter = object : InputFilter {
    override fun filter(
        source: CharSequence?,
        start: Int,
        end: Int,
        dest: Spanned?,
        dstart: Int,
        dend: Int
    ): CharSequence? {
        dest?.toString()?.replace(",", ".")?.let { s ->
                if (s.isNotEmpty() && s[0] == '0' && source != ".")
                    return ""`enter code here`
        }
        return null
    }
}

fun EditText.setAmountFilter() {
    filters = arrayOf(filterAmountValue)
}