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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Walter
  • 189
  • 2
  • 16

5 Answers5

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();
            }
Rob
  • 33
  • 5
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!!

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();
                }
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);
sanemars
  • 767
  • 5
  • 18
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+(?!$)", "")       
}
Zaid Mirza
  • 3,540
  • 2
  • 24
  • 40