android - Validating a Dialog EditText using .contains and \\W -
i have ontextchangedlistener
watches edittext see if contains "non-word" characters so;
input.addtextchangedlistener(new textwatcher() { public void aftertextchanged(editable s) {} public void beforetextchanged(charsequence s, int start, int count, int after) {} public void ontextchanged(charsequence s, int start, int before, int count) { if (input.gettext().tostring().contains("\\w")) { input.seterror("error"); } else{ } }});
however code not seem recognise ("\\w")
non-word characters. have used check other edittexts in instances replaces non-word characters without prompting works fine;
string locvalidated = textlocation.gettext().tostring().replaceall("\\w", "-");
it seem cannot use \\w
check if edittext contains such characters, replace them. there workaround this?
string.contains()
not check regular expressions. in case, checking string
"\w"
. simple (sub-)string compare.
a workaround is
string s = input.gettext().tostring(); boolean hasnonword = !s.equals(s.replaceall("\\w", "x"));
so, in case:
public void ontextchanged(charsequence s, int start, int before, int count) { string s = input.gettext().tostring(); if (!s.equals(s.replaceall("\\w", "x"))) { input.seterror("error"); } else { input.seterror(null); } }
Comments
Post a Comment