0

I would like to have EditText with some simple validation. I would like it to gain decimal numbers in format 8 symbols before comma and 2 symbols after comma.

I've found some examples here, but I can't figure out why are they not working for me. I was trying to set InputFilter, but I can't create a valid regex.

public class DecimalDigitsInputFilter implements InputFilter {

    Pattern mPattern;

    public DecimalDigitsInputFilter(final int digitsBeforeZero,
            final int digitsAfterZero) {
        mPattern = Pattern.compile("place for my regex");
    }

    @Override
    public CharSequence filter(final CharSequence source, final int start,
            final int end, final Spanned dest, final int dstart,
            final int dend) {

        Matcher matcher = mPattern.matcher(dest);
        if (!matcher.matches()) {
            return "";
        }
        return null;
    }
}
Orest
  • 1,857
  • 5
  • 19
  • 27

1 Answers1

1

A TextWatcher could be an alternative for you. See:

Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged

http://www.androidadb.com/class/android/text/TextWatcher.java.html

This way you don't have to use regex and you can check the text before it's display better.

Community
  • 1
  • 1
Thkru
  • 4,218
  • 2
  • 18
  • 37