Walter
Walter

Reputation: 189

How to disable entering numbers after zero in edittext?

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?

Upvotes: 0

Views: 2647

Answers (5)

Manish Singh
Manish Singh

Reputation: 91

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();
                }

Upvotes: 1

sanemars
sanemars

Reputation: 777

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);

Upvotes: 0

Sahdev Rajput
Sahdev Rajput

Reputation: 23

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!!

Upvotes: 1

Rob
Rob

Reputation: 33

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();
            }

Upvotes: 1

Zaid Mirza
Zaid Mirza

Reputation: 3699

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+(?!$)", "")       
}

Upvotes: 0

Related Questions