45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
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 (0–9)
|
||
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.
|
||
*
|
||
*/
|
||
|
||
}
|
||
}
|