Issue
Android keybord should not accept non-text characters such as space, enter, return etc while entering input string.
I tried with few, as shown below -
this is for the regex.
onChange = { if (it.contains(Regex("(?=\\w)"))) onInput(it) }
but in my case first character space is not accepting but in second character onwards it is accepting. I'm new this regular expression, can any one help me?
For space -> (?=\w)
value for ignore then what is the value for enter
and return
if any one knows please post here your answer ?
Update 1:
In EditText, If I enter non-string char at very first character of the input below code is working correctly:
onChange = { if (it.contains(Regex("[0123456789qwertzuiopasdfghjklyxcvbnm]"))) onInput(it) }
but If I enter string char at first place and 2nd char position onwards non-text character is accepting, so my question is how to apply same condtion for all characters of the input string ?
Thanks!
Solution
Your problem is it.contains
. That will return true if any subset of the string contains a matching regex. Which any single character will match, so any string with at least 1 letter will match it. You need to match beginning and end of string as well, to make sure it only finds a valid match across the entire string.
Also, your updated example would fail any non-english language, as it would match accented letters, letters with tildes, umlats, etc. Use ^\w*$
to match any number of letters or numbers taking up the entire string.
Answered By - Gabe Sechan
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)