How to use validation behaviors with RadEntry control

Environment

Product Version 2019.2 802.1
Product Entry for Xamarin

Description

RadEntry supports the standard Xamarin.Forms behaviors in a similar way as the Xamarin.Forms Entry. You just need to create a Behavior class and apply it to its Behaviors property.

Solution

Below you can find a simple implementation of an email validation behavior applied to RadEntry control.

First, create the custom EmailValidatorBehavior class that inherits from Behavior<RadEntry> - the TextColor of the Entry is changed depending on whether the user has entered valid email:

public class EmailValidatorBehavior : Behavior<RadEntry>
    {
    const string emailRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
    @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";


    protected override void OnAttachedTo(RadEntry bindable)
    {
        bindable.TextChanged += HandleTextChanged;
        base.OnAttachedTo(bindable);
    }

    void HandleTextChanged(object sender, TextChangedEventArgs e)
    {
        bool IsValid = false;
        IsValid = (Regex.IsMatch(e.NewTextValue, emailRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)));
        ((RadEntry)sender).TextColor = IsValid ? Color.Black : Color.Red;
    }

    protected override void OnDetachingFrom(RadEntry bindable)
    {
        bindable.TextChanged -= HandleTextChanged;
        base.OnDetachingFrom(bindable);
    }
}

Apply the above defined behavior to the RadEntry instance:

<telerikInput:RadEntry x:Name="entry" WatermarkText="Email" >
    <telerikInput:RadEntry.Behaviors>
        <local:EmailValidatorBehavior x:Name="emailValidator"/>
    </telerikInput:RadEntry.Behaviors>
</telerikInput:RadEntry>

You could learn more about behaviors in the following Xamarin blog post: Behaviors in Xamarin.Forms.

In this article
Not finding the help you need? Improve this article