简体   繁体   English

如何根据条件使文本框仅接受数字?

[英]How to make a text box to accept only number based on a condition?

How to make a textbox to accept only number? 如何使textbox仅接受数字? when my label.text is gms or rs or knot , then respective textbox accept only numbers . 当我label.textGMSRS ,那么相应的textbox只接受数字 when label.text is character it has allow only character value . label.text为字符时,仅允许字符值

Example:gms: 1200
        character:black
        knot:5
        rs:80
        character:pink

This order may change based on the selection. 该顺序可以根据选择而改变。 And please post the ASPX code too. 并且也请发布ASPX代码。

Can you add the KeyPress event for the corresponding textbox? 您可以为相应的文本框添加KeyPress事件吗? So that you can do the following! 这样您就可以执行以下操作!

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
   if ((label.Text.Equals("gms") || label.Text.Equals("rs") || label.Text.Equals("knot"))
   { 
      if (!char.IsDigit(e.KeyChar))
      {
         e.Handled = true;
      }
   }
   else
   {
      if (!char.IsLetter(e.KeyChar))
      {
         e.Handled = true;
      }
   }
}

Apply a regular validator on your text box with validation expression "^[0-9]" which accept only numbers initially make it disabled 在带有验证表达式"^[0-9]"文本框上应用常规验证器,该表达式最初仅接受数字,因此将其禁用

you can then enable disable it according to the text of your label making the text box to accept all or only numbers 然后,您可以根据标签的文本启用禁用它,使文本框接受所有或仅数字

Try this, maybe this is something that you're looking for: 试试这个,也许这是您正在寻找的东西:

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
           if ((label.Text.Equals("gms") || label.Text.Equals("rs") || label.Text.Equals("knot")))
           { 
              if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
                {
                    e.Handled = true;
                }

              // only allow one decimal point
              if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
              {
                  e.Handled = true;
              }
           }
        }

Although you want to implement the solution in . 尽管您想在实现解决方案。 Better suggestion would be is to implement in . 更好的建议是在实现。

Add simple javascript function to in aspx page and no code in codebehind file. 将简单的javascript函数添加到aspx页面中,而代码隐藏文件中没有代码。

$(document).ready(function () {

var arrForNum = ['gms', 'rs', 'knot']; //Your list of label texts for Number only textboxes
// Now traverse for all textboxes where u want to add some restrictons

$('body').find('.customonly').each(function () {
    var id = this.id;
    var res = $('label[for=' + id + ']').text();    
   // check if its the array we declared else it will be charecters only. 
    if ($.inArray(res, arrForNum) >= 0) {
        $(this).forceNumeric();  // Added simple function in fiddle.
       //You can apply any other function here if required.
    } else {
        $(this).forceCharecters('chars');
    }
  });
});

Check the JsFiddle for detailed code. 检查JsFiddle以获取详细代码。

private void txt3_KeyPress(object sender, KeyPressEventArgs e)
{

    for (int h = 58; h <= 127; h++)
    {
        if (e.KeyChar == h)             //58 to 127 is alphabets tat will be         blocked
        {
            e.Handled = true;
        }

    }

    for(int k=32;k<=47;k++)
    {
        if (e.KeyChar == k)              //32 to 47 are special characters tat will 
        {                                  be blocked
            e.Handled = true;
        }

    }



}

try this is very simple 试试这个很简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM