How do you implement real-time field validation as the user types?
Short answer
Bind TextChanged or use the view model's property setter to re-evaluate validation, expose an error message property, and show it with a bound label or DataTrigger.
Advertisement
AModel answer
Bind TextChanged or use the view model's property setter to re-evaluate validation, expose an error message property, and show it with a bound label or DataTrigger. The CommunityToolkit's validation behaviors (e.g., TextValidationBehavior, EmailValidationBehavior) can apply visual states automatically.
<Entry Text="{Binding Email}">
<Entry.Behaviors>
<toolkit:EmailValidationBehavior
InvalidStyle="{StaticResource InvalidEntry}" />
</Entry.Behaviors>
</Entry>Real-world example
An email field shows a red border immediately when the input isn't a valid address, using a validation behavior.
Likely follow-ups
- Where do validation behaviors come from? | How do you show the error text?
Related topics