I have a simple form where user add email:
<Entry Placeholder="Email" Text="{Binding Email}">
<Entry.Behaviors>
<xct:MultiValidationBehavior >
<xct:EmailValidationBehavior Flags="ValidateOnUnfocusing"/>
</xct:MultiValidationBehavior>
</Entry.Behaviors>
</Entry>
And have a button:
<Button Text="Send" Command="{Binding LoadSendRegistrationCommand}" TextColor="#07987f" BackgroundColor="#eeeeee" Margin="0,10,0,10"></Button>
When click on button how to check and return message if email is not valid?
There's (as always) multiple ways to do this. If you just want to do this from your code-behind without MVVM/data-binding you still have two options.
Since you're using the
MultiValidationBehavioryou could give that anx:Name="myValidation"attribute and access that from your code-behind. You will then be able to do:1. Through
MultivalidationBehaviorAdditionally you probably want to specify the
MultivalidationBehavior.Errorproperty on theEmailValidationBehavior, i.e.:<xct:EmailValidationBehavior xct:MultiValidationBehavior.Error="Email not valid" Flags="ValidateOnUnfocusing"/>2. Through
EmailValidationBehaviorYou can also do it with the
EmailValidationBehaviordirectly. For this add anx:Name="myEmailValidation"attribute to yourEmailValidationBehaviorand you can access it in your code-behind:3. Through data-binding
I actually noticed while typing all this that you did use data-binding with the
Commandfor that button and the value of the email. In that case, you can also bind to theIsValidproperty on either theEmailValidationBehaviororMultiValidationBehaviorand then toggle some UI element to visible or not depending on that.For this we need to do a couple of things. I will focus on the
EmailValidationBehavior, I trust that you can figure it out for theMultiValidationBehaviorone.Add the binding to your
EmailValidationBehavior:<xct:EmailValidationBehavior IsValid="{Binding EmailValid}" Flags="ValidateOnUnfocusing"/>Add a backing property to the object that is in your
BindingContext:<Label Text="Email not valid" TextColor="Red" IsVisible="{Binding EmailValid, Converter={StaticResource invertBoolConverter}}" />Notice that I need to also use the
InvertedBoolConverterfrom the Toolkit to invert the value fromIsValidto work with it correctly withIsVisibleof theLabelThat should be it :)
Working sample with all this code can be found here: https://github.com/jfversluis/XCTInputValidationCodeBehindSample