Xamarin.Forms: Validation using Behaviors

public class HolderNameValidation : Behavior
    {
        protected override void OnAttachedTo(Entry bindable)
        {
            bindable.TextChanged += OnInput;
            base.OnAttachedTo(bindable);
        }

        protected override void OnDetachingFrom(Entry bindable)
        {
            bindable.TextChanged -= OnInput;
            base.OnDetachingFrom(bindable);
        }

        void OnInput(object sender, TextChangedEventArgs e)
        {
            var bindable = sender as Entry;

            var firstName = parseFirstName(e.NewTextValue);
            var lastName =  parseLastName (e.NewTextValue);

            if (firstName.Item == "" || lastName.Item == "")
                 bindable.BackgroundColor = Theme.InvalidEntryColor;
            else bindable.BackgroundColor = Theme.ValidatedEntryColor;
        }
    }

xmlns:converters=”clr-namespace:Account.UI.Converters”

Behavior

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s