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
MultiValidationBehavior
you could give that anx:Name="myValidation"
attribute and access that from your code-behind. You will then be able to do:1. Through
MultivalidationBehavior
Additionally you probably want to specify the
MultivalidationBehavior.Error
property on theEmailValidationBehavior
, i.e.:<xct:EmailValidationBehavior xct:MultiValidationBehavior.Error="Email not valid" Flags="ValidateOnUnfocusing"/>
2. Through
EmailValidationBehavior
You can also do it with the
EmailValidationBehavior
directly. For this add anx:Name="myEmailValidation"
attribute to yourEmailValidationBehavior
and 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
Command
for that button and the value of the email. In that case, you can also bind to theIsValid
property on either theEmailValidationBehavior
orMultiValidationBehavior
and 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 theMultiValidationBehavior
one.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
InvertedBoolConverter
from the Toolkit to invert the value fromIsValid
to work with it correctly withIsVisible
of theLabel
That should be it :)
Working sample with all this code can be found here: https://github.com/jfversluis/XCTInputValidationCodeBehindSample