0

How to disable numbers entering after zero in edittext?

I have validated only for this..

 if (TextUtils.isEmpty(strBalance)) {
                    Toast.makeText(TransactionSellINActivity.this, "Amount should not be empty", Toast.LENGTH_SHORT)
                            .show();
                } else if (strBalance.equalsIgnoreCase("0")) {
                    Toast.makeText(TransactionSellINActivity.this, "Amount should not be Zero", Toast.LENGTH_SHORT)
                            .show();
                }

But if user enters 01 or 001 or 02,03 etc.,? ie number after zero? -> I want to restrict those numbers

How to handle such case?

5
  • you want 01 or 02 or you dont want 01 or 02 be clear.. Commented Feb 7, 2018 at 7:15
  • I don't want to use Commented Feb 7, 2018 at 7:16
  • have you tried using methods like startsWith and length Commented Feb 7, 2018 at 7:16
  • i have updated the answer. Commented Feb 7, 2018 at 7:17
  • see this answer stackoverflow.com/questions/2800739/… Commented Feb 7, 2018 at 7:32

5 Answers 5

1
if (TextUtils.isEmpty(strBalance)) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be empty", Toast.LENGTH_SHORT)
                        .show();
            } else if (strBalance.equalsIgnoreCase("0")) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be Zero", Toast.LENGTH_SHORT)
                        .show();
            }
            else if(strBalance.indexOf("0")!=a.length()-1)
                {
                Toast.makeText(TransactionSellINActivity.this, "Amount Not Valid", Toast.LENGTH_SHORT)
                        .show();
                }
                else{
                    Toast.makeText(TransactionSellINActivity.this, "Amount  Valid", Toast.LENGTH_SHORT)
                        .show();
                }
Sign up to request clarification or add additional context in comments.

2 Comments

This will not ptint number after 0.
Yes. Thank you so much.
1

I think you want to restrict those numbers that are starting with 0. For this you have to use .startsWith()

if (TextUtils.isEmpty(strBalance)) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be empty", Toast.LENGTH_SHORT)
                        .show();
            } else if (strBalance.startsWith("0")) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be Zero", Toast.LENGTH_SHORT)
                        .show();
            }

Comments

1

User can't add numbers after zero using this code TRY IT

Edittext.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {

            if (Edittext.getText().toString().startsWith("0")
                    && Edittext.getText().length() > 1) {

                Edittext.setText("0");
                Edittext.setSelection(Edittext.getText().toString().length());
            }
        }
    });

May be useful to you!!

Comments

0

You can do this too, let user enter zeros as many as he want. Following regex will remove leading zeros from string after then you can proceed with your string.

if (!TextUtils.isEmpty(strBalance) && Integer.parseInt(strBalance) > 0) 
{
        strBalance=strBalance.replaceFirst("^0+(?!$)", "")       
}

Comments

0
private EditText  mEt;//your edittext
private TextWatcher mMoneyWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (!s.toString.isEmpty()) {
            mPayEtValue.removeTextChangedListener(mMoneyWatcher);
            if (s.toString.charAt(0)=='0') {
              mEt.setText(s.toString.substring(1,s.length()));  
            }

            mEt.addTextChangedListener(mMoneyWatcher);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};
mEt.addTextChangedListener(mMoneyWatcher);

2 Comments

what is mPayEtValue?
@Walter sorry my mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.