0

In my app i am using good number of edittexts , each edit text will allow number and decmial number diiferently

Eg: edittext1 should hav : xxx.xx format
    edittext2 should hav : x.xx format
    edittext3 should hav : xx.xxx format
     edittex4 should hav : xx.xx format

Tried using InputFilter on edit text i used below code which i got in SO

    public class DecimalDigitsInputFilter implements InputFilter {

    Pattern mPattern;

    public DecimalDigitsInputFilter(int digitsBeforeZero,int digitsAfterZero) {
        mPattern=Pattern.compile("[0-9]{0," + (digitsBeforeZero-1) + "}+((\\.[0-9]{0," + (digitsAfterZero-1) + "})?)||(\\.)?");
    }

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

            Matcher matcher=mPattern.matcher(dest);       
            if(!matcher.matches())
                return "";
            return null;
        }

}

myText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(3,2)});

It partially solve the problem. what Iam looking is when i pass DecimalDigitsInputFilter(3,2)

user should be able to enter 3 digits and 4th letter only dot(.) should work restricting all other symbol entries and the after dot 2 decimal places.

Unable to get the correct regexp for this Any help is appreciated

hari86
  • 659
  • 2
  • 16
  • 28
  • Check [this link](http://stackoverflow.com/questions/7432083/how-to-use-edittext-ontextchanged-event-when-i-press-the-number) – TDG Aug 26 '16 at 06:14
  • Does it mean you want to also allow values like `345.` and empty values? Try `mPattern=Pattern.compile("^[0-9]{0," + (digitsBeforeZero-1) + "}(?:\\.[0-9]{0," + (digitsAfterZero-1) + "})?$");`. However, this will also allow a `.` input. If you do not want it, try using `Pattern.compile("^[0-9]{0," + (digitsBeforeZero-1) + "}(?:\\.[0-9]{1," + (digitsAfterZero-1) + "})?$");` (but this one will not allow `345.`). The anchors are redundant if you are using `matches()`, but good for those who want to test at regex testing sites. – Wiktor Stribiżew Aug 26 '16 at 06:20
  • oh u mean to say i cannot enter 345.00 also? after 3 digits except dot no number shoul be allowed..not possible using matches()? – hari86 Aug 26 '16 at 06:32
  • @WiktorStribiżew your first expression is working but when i give (4,2) the it should allow 234.22, 4th letter should be dot ..possible? – hari86 Aug 26 '16 at 06:40
  • If you remove `-1`, I think it will work - https://regex101.com/r/iQ2nH8/1. Why do you have `digitsBeforeZero-1` / `digitsAfterZero-1`? Use `digitsBeforeZero` / `digitsAfterZero`. Then `DecimalDigitsInputFilter(4,2)` will match `234.22`. – Wiktor Stribiżew Aug 26 '16 at 06:43
  • problem when i give (4,2) is it is allowing 4th letter as digit but i need it should not allow 4th digit as number except dot. so before dot is 3 ditgis and after dot 2 digits – hari86 Aug 26 '16 at 07:01
  • Sorry, your requirements are too vague. The point is that you need to use `^` anchor at the start and `$` (or `\z`) anchor at the end. If you pass `(4,2)` to your current code, the `234.22` is [not matched](https://regex101.com/r/xQ8gZ0/1) as the regex is `^[0-9]{0,3}(?:\.[0-9]{0,1})?$` – Wiktor Stribiżew Aug 26 '16 at 07:07
  • No feedback? Please clarify what you are trying to do. We gave you a lot of hints, something must be working. – Wiktor Stribiżew Aug 29 '16 at 07:58

1 Answers1

0

Pattern: "/\d{3}\.\d{2}$/"

123.34 success
12.34 success
1.34 success
123#34 failed
12.343 failed

if you want exact 3 digits make it {3} if you want 0-3 digits make it {0,3}

Verify here

Keppy
  • 471
  • 1
  • 7
  • 23