Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Validation for number and decimal point

$
0
0

Hi,

I want to validate only numbers and decimal point in Xamarin forms. In entry field i kept Keyboard="Numeric"
and in Validation class
public class ValidationBehavior : Behavior
{
//const string passwordRegex = @^[1-9]\d*(.\d+)?$;
protected override void OnAttachedTo(Entry bindable)
{
base.OnAttachedTo(bindable);
bindable.TextChanged += BindableOnTextChanged;
}

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



    private void BindableOnTextChanged(object sender, TextChangedEventArgs e)
    {
        //var number = e.NewTextValue;
        //var numberPattern = "5";
        //var entry = sender as Entry;
        //if (numberPattern.Contains(number))
        //{
        //    entry.TextColor = Color.Red;
        //}
        //else if (number == null)
        //{
        //    entry.BackgroundColor = Color.Red;
        //}

        if (!string.IsNullOrWhiteSpace(e.NewTextValue))
        {
            bool isValid = e.NewTextValue.ToCharArray().All(x => char.IsDigit(x));


                ((Entry)sender).Text = isValid ? e.NewTextValue : e.NewTextValue.Remove(e.NewTextValue.Length - 1);
                //((Entry)sender).TextColor = isValid ? Color.Default : Color.Red;


        }



    }

If i use this I cant access decimal.Please help me with this


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>