using System;
using System.Text.RegularExpressions;
using Xamarin.Forms;

namespace XamarinApp
{
    public class EntryBehaviour: Behavior<Entry>
    {
        protected override void OnAttachedTo(Entry bindable)
        {
            base.OnAttachedTo(bindable);

            bindable.TextChanged += Bindable_TextChanged;
            
        }

        private void Bindable_TextChanged(object sender, TextChangedEventArgs e)
        {
            string phoneNumberRegax = "^[0-9]{10}$";
            var entry = sender as Entry;
            if (!string.IsNullOrWhiteSpace(entry.Text))
            {
                if (Regex.IsMatch(entry.Text, phoneNumberRegax,
                     RegexOptions.IgnoreCase))
                {
                    entry.TextColor = Color.Green;
                }
                else
                    entry.TextColor = Color.Red;
            }
            else
                entry.TextColor = Color.Red;
        }

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

    }
}