I have a numberDecimal EditText
which I want to validate using a regular expression. In validation what I want is:
Before the decimal point, the maximum digit I want to enter is three and the digit should not start with zero like
2,23,342
, etc.After the decimal point, the maximum digit I want to enter is one like
.1
,.3
,.6
, etc.
So the number that I allow the user to enter is like 2.1
, 32.5
, 444.8
, 564.9
, etc.
But in my code, what happens is:
It allows the user to enter more than a three digit number before the decimal point like
3456
,4444
,5555
and after that it doesn't allow me to enter a decimal point after that.It allows me to enter
0
before the decimal point as the start of the digit.
So why does this happen, is anything wrong in the regular expression I have used? If anyone knows, please help me to solve this.
Code I have used:
weightEditText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s)
{
Pattern mPattern = Pattern.compile("^([1-9][0-9]{0,2})?(\\.[0-9]?)?$");
Matcher matcher = mPattern.matcher(s.toString());
if(!matcher.find())
{
weightEditText.setText(); // Don't know what to place
}
}
});