我需要检查用户输入的电话号码,该电话号码从0开始且长度至少为11。 我怎样才能做到这一点? 这是我的代码:
<Entry x:Name="txtPhoneNumber" Placeholder="شماره تلفن" Margin="5,10,5" FlowDirection="RightToLeft"
MaxLength="11"
Keyboard="Telephone">
<Entry.Behaviors>
<local:NumericValidationBehavior/>
<local:MaxLengthValidatorBehavior MaxLength="11"/>
</Entry.Behaviors>
</Entry>
这里是NumericValidationBehavior类:
public class NumericValidationBehavior : Behavior<Entry>
{
protected override void OnAttachedTo(Entry entry)
{
entry.TextChanged += OnEntryTextChanged;
base.OnAttachedTo(entry);
}
protected override void OnDetachingFrom(Entry entry)
{
entry.TextChanged -= OnEntryTextChanged;
base.OnDetachingFrom(entry);
}
private static void OnEntryTextChanged(object sender,TextChangedEventArgs args)
{
if (!string.IsnullOrWhiteSpace(args.NewTextvalue))
{
bool isValid = args.NewTextvalue.ToCharArray().All(x => char.IsDigit(x)); //Make sure all characters are numbers
((Entry)sender).Text = isValid ? args.NewTextvalue : args.NewTextvalue.Remove(args.NewTextvalue.Length - 1);
}
}
}