2010-12-03 7 views
1

Das Textfeld akzeptiert nur Zahlen und nur einen Dezimalpunkt.Wie akzeptiert man nur eine Dezimalstelle in einer Textbox?

Zum Beispiel enthält das Textfeld "12345.56". Wenn die Periode ein zweites Mal auf der Tastatur gedrückt wird, darf sie nicht in der Textbox erscheinen, da die Textbox bereits einen Punkt enthält.

+0

Ist das Web oder Fenster? –

+0

Windows Forms, Web Forms (ASP.NET) oder WPF/Silverlight? – decyclone

Antwort

7
[0-9]+(\.[0-9][0-9]?)? 

Gehen Sie mit regulären Ausdrücken.

3

hande KeyPress-Ereignis und unter der Annahme, seine Fenster

void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 

     if (e.KeyChar == '.') 
     { 
      if (richTextBox1.Text.Contains('.')) 
       e.Handled = true; 
     } 
    } 
2
**//Only numeric with Two decimal place comes in textbox and if user want to enter decimal again or space it will not allowed.** 

if ((e.Key >= Key.D0 && e.Key <= Key.D9 || 
       e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9 || e.Key == Key.Decimal || e.Key == Key.OemPeriod)) 
      { 
       string strkey = e.Key.ToString().Substring(e.Key.ToString().Length - 1, e.Key.ToString().Length - (e.Key.ToString().Length - 1)); 

       if (e.Key >= Key.D0 && e.Key <= Key.D9 || 
        e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) 
       { 

        TextBox tb = sender as TextBox; 
        int cursorPosLeft = tb.SelectionStart; 
        int cursorPosRight = tb.SelectionStart + tb.SelectionLength; 
        string result1 = tb.Text.Substring(0, cursorPosLeft) + strkey + tb.Text.Substring(cursorPosRight); 
        string[] parts = result1.Split('.'); 
        if (parts.Length > 1) 
        { 
         if (parts[1].Length > 2 || parts.Length > 2) 
         { 
          e.Handled = true; 
         } 
        } 
       } 

       if (((TextBox)sender).Text.Contains(".") && e.Key == Key.Decimal) 
       { 
        e.Handled = true; 
       } 
      } 
      else 
      { 
       e.Handled = true; 
      } 

      if (e.Key >= Key.A && e.Key <= Key.Z || 
        e.Key == Key.Space) 
      { 
       e.Handled = true; 
      } 

      if (Keyboard.Modifiers == ModifierKeys.Shift && e.Key == Key.OemPeriod) 
      { 
       e.Handled = true; 
      } 
0
private bool isValueEnteredExceed100(string textBoxValue, string inputText) 
{ 
    var countPeriod = textBoxValue.Count(x => x.ToString().Equals(".")); 
    if (countPeriod <= 1) 
    { 
     bool isValidNumber = AreAllValidNumericChars(inputText); 

     if (isValidNumber == true || inputText == ".") 
     { 
      double enterdValue; 
      bool returnValue = false; 
      if (textBoxValue != string.Empty || textBoxValue != "") 
      { 
       enterdValue = Convert.ToDouble(textBoxValue); 
       if (enterdValue > 0 && enterdValue <= 100) 
       { 
        returnValue = true; 
       } 
      } 
      return returnValue; 
     } 
     else 
     { 
      return isValidNumber; 
     } 
    } 
    else 
    { 
     return false; 
    } 
} 

private void AcceptedFromTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) 
{ 
    string textBoxValue = AcceptedFromTextBox.Text + e.Text; 
    if (textBoxValue == ".") 
    { 
     textBoxValue = "0" + e.Text; 
     AcceptedFromTextBox.Text = textBoxValue; 
    } 
    e.Handled = !isValueEnteredExceed100(textBoxValue, e.Text); 
    AcceptedFromTextBox.SelectionStart = AcceptedFromTextBox.Text.Length; 
} 


private bool AreAllValidNumericChars(string str) 
{ 
    Regex regex = new Regex(@"[0-9]$"); 
    return regex.IsMatch(str); 
} 
0

Ein weiteres Beispiel,

private void txtPrice_KeyPress(object sender, KeyPressEventArgs e) 
{ 

    if (txtPrice.Text.Length == 0) 
    { 
     if (e.KeyChar == '.') 
     { 
      e.Handled = true; 
     } 
    } 
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46) 
    { 
     e.Handled = true; 
    } 
    if (e.KeyChar == '.' && txtPrice.Text.IndexOf('.') > -1) 
    { 
     e.Handled = true; 
    } 
} 
0

-Code auf Keypress Ereignis von Textbox

if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') 
{ 
     e.Handled = true; 
} 
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) 
{ 
     e.Handled = true; 
} 
Verwandte Themen