TextBox不应仅接受数值和特殊字符.它可以接受字母数字值 [英] TextBox should not accept only numeric values and only special characters. It can accept alpha-numeric values
本文介绍了TextBox不应仅接受数值和特殊字符.它可以接受字母数字值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
TextBox不应仅接受数值和特殊字符.它可以接受字母数字值和字母.
请给我适当的ValidationExpression.
谢谢与问候,
Yogesh Sharma
Hi,
TextBox should not accept only numeric values and only special characters. It can accept alpha-numeric values and alphabets.
Please give me proper ValidationExpression.
Thanks & Regards,
Yogesh Sharma
推荐答案
StringBuilder newval = new StringBuilder();
foreach (char i in textbox1.Text)
{
if (char.IsLetterOrDigit(i))
{
newval.Append(i);
}
}
textbox1.Text = newval.GetString();
这应该在textbox的textchanged事件上完成.
并在textbox1_keyPress事件上
This should be done on textchanged event of textbox.
and on textbox1_keyPress event
if(!char.IsLetterOrDigit(textbox1.text)
{
e.handled= true;
}
此代码对您很有用.将代码放在一次cs文件中,然后将功能应用于文本框.
This code is usefull to you.place the code in once cs file and apply the functions to textbox.
public static bool IsNaturalNumber(string strNumber)
{
Regex objNotNaturalPattern = new Regex("[^0-9]");
Regex objNaturalPattern = new Regex("0*[1-9][0-9]*");
return !objNotNaturalPattern.IsMatch(strNumber) &&
objNaturalPattern.IsMatch(strNumber);
}
// Function to test for Positive Integers with zero inclusive
public static bool IsWholeNumber(String strNumber)
{
Regex objNotWholePattern = new Regex("[^0-9]");
return !objNotWholePattern.IsMatch(strNumber);
}
// Function to Test for Integers both Positive & Negative
public static bool IsInteger(String strNumber)
{
Regex objNotIntPattern = new Regex("[^0-9-]");
Regex objIntPattern = new Regex("^-[0-9]+
| ^ [0-9] +
|^[0-9]+
这篇关于TextBox不应仅接受数值和特殊字符.它可以接受字母数字值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文