限制在文本框中输入字符 [英] Restrict entering character in textbox
本文介绍了限制在文本框中输入字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何限制用户在文本框中输入字符,该文本框专门用于输入移动号码.
How to restrict a user entering character in textbox,the textbox is exclusively for entering Mobile no.
推荐答案
private void txtType1_KeyPress(object sender, KeyPressEventArgs e)
{
int isNumber = 0;
e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);
}
或:
or:
private void txtType2_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
e.Handled = true;
}
看到这里:
http://www.vcskicks.com/numbers_only_textbox.php [
See here:
http://www.vcskicks.com/numbers_only_textbox.php[^]
Bhavna
尝试简单的解决方法
Hi, Bhavna
Try a easy solution
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 65 && e.KeyChar <= 91) || (e.KeyChar >= 97 && e.KeyChar <= 123))
{
if (i && e.KeyChar == 69)
{
i = false;
}
else if (e.KeyChar!=69)
{
e.Handled = true;
}
}
}
还有更多方法可以找到,它的工作
there are some more ways you should find out, it''s work
使用正则表达式
跟随链接
http://www.dotnetspider.com/forum/140521-How-restrict-my-User-enter-only-characters-n.aspx [ http://aspdotnet-suresh.blogspot.com/2010/12/how-to-restrict-user-to-enter-only.html [ http://support.microsoft.com/kb/198649 [
Use Regular expressions
Follow the links
http://www.dotnetspider.com/forum/140521-How-restrict-my-User-enter-only-characters-n.aspx[^]
http://aspdotnet-suresh.blogspot.com/2010/12/how-to-restrict-user-to-enter-only.html[^]
http://support.microsoft.com/kb/198649[^]
这篇关于限制在文本框中输入字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文