Textbox of type number has unreliable change event

Steps
Create a textbox of type number
Try entering - + e along with digits
The change event is not triggered in some cases.

Clone link:
https://anvil.works/build#clone:WWVMXCVQ5BWPKYEK=UW7TU5I3RKNPAHDGGJPYUEZS

I don’t see an issue here other than what you are asking the textbox to do.
My understanding is that the textbox is only firing the change event when a valid number is supplied.

For example, 2 or 7.669 or -24 or 3e2 are all valid numbers and they fire the event correctly

I don’t think that giving the textbox a mathematic equation (like 3-2) qualifies as a valid number.
If you wanted to do this you should use multiple text boxes to to accept the terms in the equation or a single text textbox and parse the string into an equation.

Can you give an example of a number that is valid but doesn’t fire the change event? If so that would definitely be a bug.

You can also change your code to be:

    def input1_change(self, **event_args):
        self.from_input1.text = str(self.input1.text)

You’ll see that the change event is firing and you’ll get None whenever the value is invalid.
If you change the input from one invalid value to another invalid value, then there is no change to the number, and so the change event doesn’t fire.

(setting the text property of a component to None generally displays an empty string)

2 Likes

Thanks for feedback.
Here is an updated clone link: Anvil | Login
If you try entering invalid numbers in the top textbox that is set to type number, you will see that the change event does not always fire for invalid entries.

In setting the textbox type to number, I was expecting that only valid numbers could be entered.
I am wanting the user to only be able to enter valid numbers.
What is the best way of going about ensuring this?

I haven’t looked at the clone, but setting up a validator is a good way of controlling user input.

There are many if you search in the forum, but here is a good one that has an number check:

1 Like

Many thanks anthonys.

1 Like