C# 如何使文本框只接受基于条件的数字?
如何使C# 如何使文本框只接受基于条件的数字?,c#,asp.net,c#-4.0,javascript,C#,Asp.net,C# 4.0,Javascript,如何使文本框只接受数字? 当我的标签.text为gms或rs或结时,相应的文本框仅接受数字。 当label.text是字符时,它只允许字符值 Example:gms: 1200 character:black knot:5 rs:80 character:pink 此顺序可能会根据选择而更改。 也请发布ASPX代码。在您的文本框上应用常规验证程序,验证表达式“^[0-9]”只接受数字 最初将其禁用 然后,您可以根据标签的文本启用或
文本框
只接受数字?
当我的标签.text
为gms或rs或结时,相应的文本框
仅接受数字。
当label.text
是字符时,它只允许字符值
Example:gms: 1200
character:black
knot:5
rs:80
character:pink
此顺序可能会根据选择而更改。
也请发布ASPX代码。在您的文本框上应用常规验证程序,验证表达式
“^[0-9]”
只接受数字
最初将其禁用
然后,您可以根据标签的文本启用或禁用它,使文本框接受所有或仅接受数字您可以为相应的文本框添加按键事件吗? 这样你就可以做到以下几点了
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;
}
}
}
试试这个,也许这是你要找的东西:
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;
}
}
}
尽管您希望在中实现解决方案。更好的建议是在中国实施 在
aspx
页面中添加简单的javascript函数,并且代码隐藏文件中没有代码
$(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');
}
});
});
查看详细的代码 private void txt3\u按键(对象发送器,按键事件参数e)
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;
}
}
}
{
对于(int h=58;h请用适当的词框出问题。如果你想让gms rs知道,请看这个,然后制作一个选择框,并将它们放在文本框中。如果你的要求与此不符,请告诉我。你想在哪里检查javascript或c#中的值。请将你的html也放在这里。我要检查这个c#hi@vanet我有一个疑问。in.aspx页面我应该添加textbox\u keypress属性。如果我也要添加,请发布代码,该示例适用于windows应用程序!-(如何从ASPX页面调用此text\u keypress函数这里有一个示例:@Sylca:我认为在web应用程序中不能调用KeyPressEventArgs(System.Web对此没有定义,它是System.Windows.Forms的成员)。。。