Unfortunately, there doesn't seem to be a good way to filter input from the keyboard like there was in traditional Android. Any use of regex to remove values will place your cursor in the wrong spot, especially if someone is editing the string from the middle.
Instead, what I've seen suggested (one sample answer here), is to show an error when the user inputs invalid content.
I used a regex filter from this answer to show an error when emoji is present.
fun containsEmoji(emailStr: String?) =
Pattern
.compile("\\p{So}+", Pattern.CASE_INSENSITIVE)
.matcher(emailStr.toString()).find()
var emailText by remember { mutableStateOf(TextFieldValue("")) }
var containsEmoji by remember { mutableStateOf(false) }
Column {
TextField(
modifier = Modifier.fillMaxWidth(),
value = emailText,
onValueChange = {
emailText = it
containsEmoji = containsEmoji(it.text)
},
label = { Text(text = "Your message here") }
)
if (containsEmoji) {
Text("Please remove emoji!")
}
}
