Forum Xamarin.Forms

Is there a way to prevent a change to an Entry's Text property if newly entered value is invalid?

I have a numeric entry where I want to restrict the user from entering a value with more than 2 decimals. So if the user currently has "0.12" in the Entry, then types a 3, nothing happens and the value "0.12" remains in the field. I already have this working fine as far as what the user sees through the use of a Behavior that handles the TextChanged event of the Entry that will remove the last character of the Text property if there are more than 2 digits after the decimal.

However, the view model property bound to the Entry gets changed twice: once for user input of the 3rd decimal and once for when the Behavior changes the Entry's Text property. I do not want this to happen as the value of "0.123" immediately gets changed and should never be used, meaning that, ideally, the view model property gets changed zero times.

Is there a way to process the new value of "0.123" and prevent a change to the Entry's Text property altogether? My first thought would be to use a "TextChanging" event that can be canceled, but no such thing exists. In fact, not even the "PropertyChanging" event allows for this type of functionality.


Sign In or Register to comment.