textbox validation for allow one “ . ” value c#

后端 未结 8 1610
悲&欢浪女
悲&欢浪女 2021-01-23 08:56

I want textbox validation for allowing only one . value and only numbers. Means my textbox value should take only numerics and one . value. Value shoul

相关标签:
8条回答
  • 2021-01-23 09:36

    Also try this short one

    e.Handled = (!(e.KeyChar == (char)Keys.Back || e.KeyChar == '.')); //allow  dot and Backspace
    e.Handled = (e.KeyChar == '.' && TextBox1.Text.Contains(".")); //allow only one dot
    

    this example only allow one dot and backspace

    0 讨论(0)
  • 2021-01-23 09:40
     if (textBox.Text!="")
            {
                string txt = textBox.Text;
                if (e.KeyChar.ToString().Any(Char.IsNumber) || e.KeyChar == '.')
                {
                    textBox.Text = rate;
                }
                else
                {
                    MessageBox.Show("Number Only", "Warning");
                    textBox.Text = "";
                }
            }
    

    My tested code

    0 讨论(0)
  • 2021-01-23 09:48

    try this one

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                   && !char.IsDigit(e.KeyChar)
                   && e.KeyChar != '.')
                e.Handled = true;
    
            // only allow one decimal point
            if (e.KeyChar == '.'
                && textBox1.Text.IndexOf('.') > -1)
                e.Handled = true;
        }
    
    0 讨论(0)
  • 2021-01-23 09:49

    try this code and just replace what you want input type 'validinpu' string.

    try
    {
        short charCode = (short)Strings.Asc(e.KeyChar);
        string validinput = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789 .";
        if (Strings.InStr(validamt, Conversions.ToString(Strings.Chr(charCode)), Microsoft.VisualBasic.CompareMethod.Binary) == 0)
        {
            charCode = 0;
        }
        if (charCode == 0)
        {
            e.Handled = true;
        }
    }
    
    0 讨论(0)
  • 2021-01-23 09:50
    if(e.KeyChar.Equals('\b'))
     {
      e.Handled = false;
     }
    else
     if (!char.IsControl(e.KeyChar)
               && !char.IsDigit(e.KeyChar)
               && e.KeyChar != '.')
      {
            e.Handled = true;
      }
     else
        // only allow one decimal point
        if (e.KeyChar == '.'
            && textBox1.Text.IndexOf('.') > -1)
      {        
        e.Handled = true;  
      }
    
    0 讨论(0)
  • 2021-01-23 09:51

    Another example ,

     private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
             // To disallow typing in the beginning writing
            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 讨论(0)
提交回复
热议问题