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