Dirol
Dirol

Reputation: 374

Validate EditText

I've stucked with the following problem: I need to validate, that EditText's value is an Integer between 1 and 1000. If it's not - I should put some of the default value into the EditText. When I try to do such a thing with following code - I get to the infinite loop (well, that was predictable). I've checked the similar questions, but I still can't figure out, how to make the code working.
Are there any other ways to implement the desirable behaviour? How I should edit my code to achieve this?

    Long mailIntervalValue;
    EditText etMailInterval;
        .
        .
        .
        .
    etMailInterval=(EditText)findViewById(R.id.et_mail_check_interval);
    etMailInterval.setText(mailIntervalValue.toString());
    etMailInterval.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {
            Integer t=Integer.getInteger(s.toString());
            if (t==null){
                s.clear();
                s.append(mailIntervalValue.toString());
                mailIntervalValue=MessageManager.DEFAULT_TIME;
            }
            else
                mailIntervalValue=t.longValue();
            if (mailIntervalValue<1 || mailIntervalValue>1000){
                if (mailIntervalValue<1)
                    mailIntervalValue=1L;
                else
                    mailIntervalValue=1000L;
                s.clear();
                s.append(mailIntervalValue.toString());
                Toast.makeText(MainActivity.this, MainActivity.this.getString(R.string.settings_timer_fail),
                        Toast.LENGTH_SHORT).show();
            }

            saveMailerPrefs();
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });

Upvotes: 0

Views: 1223

Answers (3)

zasadnyy
zasadnyy

Reputation: 2167

You can use z-validations library for your case, there is InRange validation - it's exactly what you need. Sample code using library:

//create validation form
Form form = new Form(mActivity);
form.addField(Field.using(mYourEditText).validate(InRange.build(mContext, minAllowed, maxAllowed)));

//validate form

if (form.isValid()) {
    Toast.makeText(this, "Form is valid", Toast.LENGTH_SHORT).show();
}

More about EditText validation and this library you can read in post: Android form validation - the right way

Upvotes: 0

James Lim
James Lim

Reputation: 13054

I suggest using the intended tool for the job: an input filter might be a better alternative.

Here is some example code and some documentation. If you need something more complex, I recommend looking at this code.

Upvotes: 1

Walid Hossain
Walid Hossain

Reputation: 2714

Did you notice that TextWatcher is calling itself? I think it would be a better approach to check validation with any other event e.g. On Enter press or with a button etc..

Upvotes: 0

Related Questions