2013-05-07 83 views
1

我有一個輸入框應該只允許數字/貨幣。爲此,我使用InputScope「CurrencyAmount」。如何限制數字鍵盤輸入只允許單個小數點

當我運行的代碼數字鍵盤就會彈出,但用戶被允許進入許多小數點,而不是僅僅一個

例如: 應該允許在文本框中輸入類似「12.50」的輸入,但是用戶可以輸入像「12 .... 50」,「。12.5 .... 0」等值。

如何限制允許的文本框值符合我的標準?

+0

驗證輸入? – vcsjones 2013-05-07 20:56:39

回答

2

我會追加一個按鍵事件處理程序到您的文本框,並驗證您的輸入是否與您的謂詞匹配。

僞代碼:

//... 
//register event handler 
yourTextBox.KeyDown += new KeyEventHandler(yourTextBox_KeyDown); 
//... 

//the keydown event 
public void yourTextBox_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (System.Text.RegularExpressions.Regex.IsMatch(yourTextBox.Text,"<enter a regular expression here>")) 
     e.Handled = true; 
    else e.Handled = false; 
} 
+0

我是C#的初學者。你介意解釋在哪裏註冊事件處理程序?它是否進入公共無效Initialize()方法?謝謝,馬修。 – 2013-05-08 14:18:04

+0

我會將它放到頁面的構造函數中 – MUG4N 2013-05-08 22:25:02

2

你可以嘗試訂閱TextChanged事件的文本框和下面的驗證運行 - 非常適用於其他語言環境過於除了EN-US。

string decimalsep = CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator; 
int decimalSepCount = text1.Text.Count(f => f == decimalsep[0]); 
if (decimalSepCount > 1) 
{ 
    MessageBox.Show("Invalid input"); 
} 
+0

這可以完美地驗證我的輸入,但我也意味着如何物理阻止用戶在我的GUI文本框中輸入小數點。我是C#的初學者,除了(f => f == decimalsep [0])外,我完全理解你的代碼。你介意解釋嗎?謝謝你,馬修。 – 2013-05-08 14:13:02

+0

在Windows Phone中,除了選擇鍵盤佈局(如數字,文本,郵件,URL等)之外,您無法控制鍵盤輸入的行爲 - 目前不支持。在另一個問題上,它只是計算文本框中的小數點分隔符(使用Linq)。 – Mahender 2013-05-08 14:18:44

+0

我見過的應用程序只允許用戶輸入一個小數點。他們是如何實現這一點的? – 2013-05-08 14:55:08

3

我會去行爲和正則表達式。然後,您可以輕鬆地將您的代碼重新用於其他文本框。

public class RegexValidationBehavior : Behavior<TextBox> 
{ 
    public static readonly DependencyProperty RegexStringProperty = 
     DependencyProperty.Register("RegexString", typeof(string), typeof(RegexValidationBehavior), new PropertyMetadata(string.Empty)); 

    public string RegexString 
    { 
     get { return GetValue(RegexStringProperty) as string; } 
     set { SetValue(RegexStringProperty, value); } 
    }  

    protected override void OnAttached() 
    { 
     base.OnAttached(); 

     if (AssociatedObject != null) 
     { 
      AssociatedObject.TextChanged += OnTextChanged; 
     } 
     Validate(); 
    } 

    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 

     if (AssociatedObject != null) 
     { 
      AssociatedObject.TextChanged -= OnTextChanged; 
     } 
    } 

    private void OnTextChanged(object sender, TextChangedEventArgs e) 
    { 
     Validate(); 
    } 

    private void Validate() 
    { 
     var value = AssociatedObject.Text; 
     if (value.IsNotEmpty() && RegexString.IsNotEmpty()) 
     { 
      MatchAgainstRegex(value); 
     } 
    } 

    private void MatchAgainstRegex(string value) 
    {    
     var match = Regex.Match(value, RegexString); 
     if (!match.Success) 
     { 
      AssociatedObject.Text = value.Remove(value.Length - 1); 
      AssociatedObject.Select(AssociatedObject.Text.Length, 0); 
     }   
    } 
} 

然後在你的XAML中寫下類似的東西。

<TextBox InputScope="Number" Text="{Binding Amount, Mode=TwoWay}"> 
    <i:Interaction.Behaviors> 
     <Control:RegexValidationBehavior RegexString="{Binding OnlyTwoDecimalsRegex}"/> 
    </i:Interaction.Behaviors> 
</TextBox> 

在您的視圖模型您指定的正則表達式,例如

public string OnlyTwoDecimalsRegex { get { return @"^([0-9]+)?([,|\.])?([0-9]{1,2})?$"; } }