Question

I want to restrict the user of my app entering values in an EditText. The values should look like this: 3/54/32

First number: 0..13 Middle number: 0..7 Last number: 0..255

The slashs should be shown fix in the EditText!

I already have an InputFilter for IP address but I do not understand it... ;-)

 //Use a input filter for the input of the IP address
            InputFilter[] filters = new InputFilter[1];
            filters[0] = new InputFilter() {
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                        if (end > start) {
                            String destTxt = dest.toString();
                            String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
                            if (!resultingTxt.matches ("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) { 
                                return "";
                            } else {
                                String[] splits = resultingTxt.split("\\.");
                                for (int i=0; i<splits.length; i++) {
                                    if (Integer.valueOf(splits[i]) > 255) {
                                        return "";
                                    }
                                }
                            }
                        }
                 return null;
                 }
            };                  
            et_router_ip.setFilters(filters);

Would be great if someone could help me!

Was it helpful?

Solution

It'S batter if you divide your Edit Text by 3 Different input view.

For that Prepare your general function in which you have to pass minimum input value , maximum input value and your Edit Text Obj Like :

public void Edit_Validation(int MinLen, int MaxLen, EditText edt)
  throws NumberFormatException {
if (edt.getText().toString().length() <= 0) {
  edt.setError("Required.");
  data = null;
} else if (Double.valueOf(edt.getText().toString()) < MinLen
    || Double.valueOf(edt.getText().toString()) > MaxLen) {
  edt.setError("Out of Range " + MinLen + " or " + MaxLen);
  data = null;
} else {
  data = edt.getText().toString();
} } // END OF Edittext validation

Now you check current value When user typing in Edit Text using TextWatcher .

// EditText Validateion
Your_First_EditText.addTextChangedListener(new TextWatcher() {
  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count) {

  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count, int after) {

  }

  @Override
  public void afterTextChanged(Editable s) {
    try {

      Edit_Validation(0,13, Your_First_EditText);


    } catch (Exception e) {
      Log.e("Invalid Value", "Negative Value");
    }

  }
});

Now same way you can call Edit_Validation(...) in your next two Edit Text Watcher.

I think it will be easy and time consuming. i also use this same approach in my one of the application.

And if you get accurate answer then share with me .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top