I am trying to put some constraints on my EditText field in my app. For the fullname and some other fields, I would like to only allow letters, space and some other characters like '.' or '-'.
I saw some stuff online and put this together:
mFullnameView = (EditText) findViewById(R.id.activity_register_et_fullname);
TextWatcher fullnameTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
validate(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//
}
private void validate(String s) {
if (!(Pattern.matches("^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$", s))) {
if (s.length() > 0) {
final String cleanText = s.substring(0, s.length());
mFullnameView.setText(cleanText);
}
}
}
};
However now my app freezes for a few seconds and then crashed when I try typing a "." in the EditText field, I do not know why this happens or how to resolve it.