I am working on customizing TextField in Jetpack Compose, I want restrict the input from the user to numbers, commas or a single dot.
The regular expression that I am currently using is val expression = Regex("[\\d,.]+")
. It works fine for numbers and commas but I am able to enter multiple dots(.) which I don't want. I want one single dot only.
Here is my Jetpack Compose code.
val expression = Regex("[\\d,.]+")
val keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
)
TextField(
value = text,
onValueChange = {
if (it.isEmpty() || it.matches(expression))
onValueChange(it)
},
keyboardOptions = keyboardOptions,
keyboardActions = KeyboardActions(
onDone = { }
),
)