Add project files.
This commit is contained in:
44
Gready_Poang.Common/Specifics/DigitsOnlyBehavior.cs
Normal file
44
Gready_Poang.Common/Specifics/DigitsOnlyBehavior.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace GreadyPoang.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.
|
||||
*
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user