AndroidDev
AndroidDev

Reputation: 4559

How to perform validation in EditText

I have EditBox where I have to check if the number entered by the user is correct or not. For doing so I want to call such an event that allows the user to type a number in the EditBox whenever the number is correct, otherwise, if the number is not correct it doesn't allow the user to type in the EditBox. How That can be done? Any example will be helpful for me.

Upvotes: 0

Views: 2166

Answers (4)

sush
sush

Reputation: 476

you can use 2 ways
1. textwatcher
2. Inputfilter
i am attaching the code which includes both

public class MainActivity extends Activity {   
EditText editTxt;  
private TextView regresult;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    editTxt =(EditText)findViewById(R.id.editID);
    regresult = (TextView)findViewById(R.id.txtID);
    String urName=editTxt.getText().toString();

    editTxt.setFilters(new InputFilter[]{new DecimalDigitsInputFilter()});      
    editTxt.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 s) {
              if (editTxt.getText().toString().matches("(^([0-9]{0,3})?)(\\.[0-9]{0,1})?$"))
                {
                    regresult.setText("");
                }
                else
                {
                    regresult.setText("invalid number");
                }
        }
    });
}}  


class DecimalDigitsInputFilter implements InputFilter
{   
Pattern mPattern;

public DecimalDigitsInputFilter()
{
    mPattern = Pattern.compile("(^([0-9]{0,2})?)(\\.[0-9]{0,1})?$"); //here u can give your required pattern
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    // TODO Auto-generated method stub
     Matcher matcher = mPattern.matcher(dest);
        if(!matcher.matches())
        {
            return "";
        }
        return null;
}}

Upvotes: 0

C. Leung
C. Leung

Reputation: 6318

final Pattern pattern = Pattern.compile("[0-9]");
editText.addTextChangedListener(new TextWatcher() {

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        Matcher matcher = pattern.matcher(s);
        if (matcher.find())
        {
            s.replace(0, s.length(), s.toString().replaceAll("[0-9]", ""));
        }
    }

    public void beforeTextChanged(CharSequence s, int start,
            int before, int count) {
        // TODO Auto-generated method stub
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub
    }

});

Upvotes: 2

Bhavin
Bhavin

Reputation: 6010

You will have to use TextWatcher.

You can Just Go HERE. Example is also given here, you can refer it.

Upvotes: 1

ngesh
ngesh

Reputation: 13501

use onTextChangedListener() and validate inside its beforeTextChangedMethod() .. something like this..

 ((EditText)findViewById(R.id.et_testo)).addTextChangedListener(new TextWatcher() {

    public void afterTextChanged(Editable s) {
        ((TextView)findViewById(R.id.numcaratteri)).setText(String.format(getString(R.string.caratteri), s.length()));

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // Validate here

    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub

    }

});

Upvotes: 4

Related Questions