2013-09-21 35 views
0

如何使textbox只接受数字? 当我label.textRS,然后各自textbox只接受数字。 当label.text是字符它只允许字符值如何使文本框只接受基于条件的数字?

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

此订单可能会根据选择而改变。 请发布ASPX代码。

+0

请框架的问题在适当的话。 – Ankur

+0

看看这个http://stackoverflow.com/questions/9732455/how-to-allow-only-integers-in-a-textbox – Adrian

+0

如果你想gms rs知道然后做一个选择框,并把它们放到文本框。如果您的要求不符合,请告诉我。 – Adrian

回答

0

申请定期校验与验证表达"^[0-9]"其只接受数字文本框 最初使残疾人

您就可以启用根据您的标签的文本进行文本框接受所有禁用或只有号码

1

您可以为相应的文本框添加KeyPress事件吗? 这样你就可以做到以下几点!

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; 
     } 
    } 
} 
+0

hi @vanest我有一个疑问。在.aspx页面上我应该添加textbox_keypress属性。如果我还要补充一点,也请张贴该编码 – Gopi

+0

请发帖.aspx代码 – Gopi

+0

此示例是为Windows应用程序! :-( – Vanest

0

试试这个,也许这就是你正在寻找的东西:

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; 
       } 
      } 
     } 
+0

如何从ASPX页面调用此text_keypress函数页面 – Gopi

+0

下面是一个示例:http://stackoverflow.com/questions/13158752/fire-event-on-enter-key-press-for-a-asp-net -textbox – Sylca

+0

@Sylca:我认为KeyPressEventArgs不能在Web应用程序中调用(System.Web没有定义它,它是System.Windows.Forms的成员)... – Vanest

0

虽然要实现解决方案。更好的建议是在执行。

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'); 
    } 
    }); 
}); 

检查JsFiddle的详细代码。

0
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; 
     } 

    } 



} 

试试这个非常简单

相关问题