Files
Gready_Poang/Gready_Poang.Common/Specifics/DigitsOnlyBehavior.cs

45 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Text.RegularExpressions;
namespace Gready_Poang.Common;
public class DigitsOnlyBehavior : Behavior<Entry?>
{
protected override void OnAttachedTo(Entry? entry)
{
if (entry != null)
{
entry.TextChanged += OnTextChanged;
}
base.OnAttachedTo(entry);
}
protected override void OnDetachingFrom(Entry? entry)
{
if (entry != null)
{
entry.TextChanged -= OnTextChanged;
}
base.OnDetachingFrom(entry);
}
private void OnTextChanged(object? sender, TextChangedEventArgs e)
{
var entry = sender as Entry;
if (entry == null) return;
// Tillåt endast siffror (09)
if (!Regex.IsMatch(e.NewTextValue, @"^\d*$"))
{
entry.Text = e.OldTextValue; // Återställ till tidigare giltigt värde
}
/*
* Vill du tillåta decimaler? Ändra regex till @"^\d*\.?\d*$"
Vill du tillåta negativa tal? Använd @"^-?\d*$"
Vill du visa en varning när användaren skriver fel?
Lägg till en BindableProperty för IsValid och bind den till UI.
*
*/
}
}