Can be solved by using filters
, i.e.
val regex = Regex("^[~#^|$%&*!]*$")
val filter = InputFilter { source, _, _, _, _, _ ->
return@InputFilter when {
source?.matches(regex) == true -> ""
else -> null
}
}
editText.filters = arrayOf(filter)
and for handling copy-pasted
text, can be done by adding condition to check if text inputted was greater than 1
, which now looks like this
val regex = Regex("^[~#^|$%&*!]*$")
val filter = InputFilter { source, _, _, _, _, _ ->
return@InputFilter when {
source?.matches(regex) == true -> ""
source.length > 1 -> source.trim { it.toString().matches(regex) } // Put your desired logic here, these sample logic was doing a trim/remove
else -> null
}
}
editText.filters = arrayOf(filter)