2017-09-09 25 views
0

数字を受け付けるエントリに文字列を入力できないようにするには、以下のコードを使用しています。文字列だけを受け入れ、数字を受け入れる必要のないエントリに対して、これをどのように逆にすることができますか?xamarinフォームの動作:数字ではなくエントリ内の文字列のみを許可する

  private static void OnEntryTextChanged(object sender, TextChangedEventArgs args) { 

       if(!string.IsNullOrWhiteSpace(args.NewTextValue)) { 
        bool isValid = args.NewTextValue.ToCharArray().All(IsDigit); //Make sure all characters are numbers 

        ((Entry)sender).Text = isValid ? args.NewTextValue : args.NewTextValue.Remove(args.NewTextValue.Length - 1); 
       } 
      } 

      /// <summary> 
      /// Check to see if a character is a digit. 
      /// </summary> 
      /// <param name="c">The character to check.</param> 
      /// <returns><c>true</c> if the character is between <c>0</c> and <c>9</c>.</returns> 
      private static bool IsDigit(char c) { 
       if(c >= 48) { 
        return c <= 57; 
       } 

       return false; 
      } 
+0

bool isValid = args.NewTextValue.ToCharArray()の呼び出しを置き換えます。すべて(IsDigit); ToCharArray()ToCharArray()。All(char.IsLetter); args.NewTextValue.ToCharArray –

+0

このコードは、カーソルの位置が最後にない場合は検証されません。最後の文字のみが削除されます。新しいキャラクターではない – Ada

答えて

0

正規表現を使用する必要があります。この正規表現は、文字のみを許可:

あなたはこのように書く必要がありますあなたの方法で今
^[a-zA-Z]+$ 

const string numberRegex = @"^[a-zA-Z]+$"; 
private static void OnEntryTextChanged(object sender, TextChangedEventArgs args) 
{ 
    if(!string.IsNullOrWhiteSpace(args.NewTextValue)) 
    { 
     IsValid = (Regex.IsMatch(args.NewTextValue, numberRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250))); 
     ((Entry)sender).Text = IsValid ? args.NewTextValue : args.NewTextValue.Remove(args.NewTextValue.Length -1);  
    } 
} 

あなたBehaviorが正しいかどうかはわからないが、あなたはこのようにそれを作成することができます。

<Entry Placeholder="Paste here your value" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand"> 
    <Entry.Behaviors> 
      <validator:EntryValidatorBehavior x:Name="numberValidator"/> 
    </Entry.Behaviors> 
</Entry> 
:このような
public class EntryValidatorBehavior: Behavior<Entry> 
{ 
    const string numberRegex = @"^[a-zA-Z]+$"; 

    static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(EmailValidatorBehavior), false); 

    public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty; 

    public bool IsValid 
    { 
     get { return (bool)base.GetValue(IsValidProperty); } 
     private set { base.SetValue(IsValidPropertyKey, value); } 
    } 

    protected override void OnAttachedTo(Entry bindable) 
    { 
     bindable.TextChanged += HandleTextChanged; 
    } 

    void HandleTextChanged(object sender, TextChangedEventArgs e) 
    { 
     IsValid = (Regex.IsMatch(e.NewTextValue, numberRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250))); 
     ((Entry)sender).Text = IsValid ? e.NewTextValue : e.NewTextValue.Remove(e.NewTextValue.Length -1); 
    } 

    protected override void OnDetachingFrom(Entry bindable) 
    { 
     bindable.TextChanged -= HandleTextChanged; 
    } 
} 

そして、あなたのビュー(XAMLファイル)で

関連する問題