I am confused about the pattern used in my Regular Expression. What I want is before decimal the max no of digit the user want to enter is three and the digit should not start with zero. After entering the digit before decimal point which can be three-digit or two-digit or one-digit but can not start with zero. I want to allow users to enter one digit after the decimal point and it should be no more than one. So what will be the pattern for these?
Example:
Number Allow to enter: 1.3
, 22.3
, 333.4
, 999.6
Number Not allowed to enter: 0
, 0.1
, .1
, 4444.67
, 333.78
Code I have Used
tempEditText.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(3,1)});
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;
}
}