Organizational Research By

Surprising Reserch Topic

filter conditions on editext using java pattern api using -'java,android'


filter conditions on editext using java pattern api  using -'java,android'

I have the following requirement.

User can only enter any value on the following range on an Android EditText which accepts numbers and decimals but not negatives.

Minimum value: 0

Max value: 10

Intermediate values(between max and Min) can be 0.1 , 0.2 , 0.3 .....    9.8 , 9.9 ,10

I have used the Java Pattern for the implementation.

int digitsBeforeZero = 2;
int digitsAfterZero = 1;


    public class DecimalDigitsInputFilter implements InputFilter {

            private 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;
            }

        }


The above code works for the following conditions.

i> For value 10 and above, it doesn't accept any decimals , which is correct.

ii> It accepts one number after decimal for the range 0.0 - 9.9 which is also correct.

But it accepts values from 0 , 0.1 , 0.2 ..... 9.8 , 9.9 , 10 , 11 , 12 ........... 99

How to restrict the value to 10 with the existing conditions?

Any suggestions will be helpful.
    
asked Sep 7, 2015 by rajesh
0 votes
9 views



Related Hot Questions



Government Jobs Opening


...