验证只能输入数字和逗号 有一个文本框,要求验证,只能输入数字和逗号,想写一个onkeypress事件,如何些? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void txtAge_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8) { e.Handled = true; } } 正则式,onKeypress="return (/,|\d/.test(String.fromCharCode(event.keyCode)))") 1.可以验证Ascii 例如1楼2.可以用正则 例如2楼 最近CSDN好像变成了大家帮忙做作业的地方了 private void tBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 0x20) e.KeyChar = (char)0; //禁止空格键 if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return; //处理负数 if (e.KeyChar > 0x20) { try { double.Parse(((TextBox)sender).Text + e.KeyChar.ToString()); } catch { e.KeyChar = (char)0; //处理非法字符 } } }private void TextBox_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar)) { e.Handled = true; } }或者private void TextBox_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar)) { e.Handled = true; }}private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e){if(e.KeyChar!='\b')//这是允许输入退格键{if((e.KeyChar<'0')||(e.KeyChar>'9'))//这是允许输入0-9数字{e.Handled = true;}}}private void button1_Click(object sender, EventArgs e) { string text = this.textBox1.Text; if (text != null) MessageBox.Show(text); }private void textBox1_Validating(object sender, CancelEventArgs e) { const string pattern = @"^\d+\.?\d+$"; string content = ((TextBox)sender).Text;if (!(Regex.IsMatch(content, pattern))) { errorProvider1.SetError((Control)sender, "只能输入数字!"); e.Cancel = true; } else errorProvider1.SetError((Control)sender, null); }private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e){if(e.KeyChar=='.' && this.textBox1.Text.IndexOf(".")!=-1){e.Handled=true;}if(!((e.KeyChar>=48 && e.KeyChar<=57) || e.KeyChar=='.' || e.KeyChar==8)){e.Handled=true;}}private void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e) { if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar)) { e.Handled = true;//消除不合适字符 } else if (Char.IsPunctuation(e.KeyChar)) { if (e.KeyChar != '.' || this.textBox1.Text.Length == 0)//小数点 { e.Handled = true; } if (textBox1.Text.LastIndexOf('.') != -1) { e.Handled = true; } } }利用ASCII码处理办法、{ if ((e.KeyChar <= 48 || e.KeyChar >=57) && (e.KeyChar != 8) && (e.KeyChar != 46)) e.Handled = true;================48代表0,57代表9,8代表空格,46代表小数点} 献出本人的只能输入小数点和数字的方法你可以参考一下。不能光用Keypress,比如退格键时不执行keypress,有时在按下退格键时,要验证删除后数据格式是否正确。#region " 控制文本框类型输入" /// <summary> /// 押金文本框KeyDown事件 /// </summary> /// <param name="sender">押金文本框</param> /// <param name="e">按键KeyDown事件数据</param> /// <res></res> private void TextBoxEx_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { // 如果不是小数模式,退出 if (enumInputType != InputTypeEnum.Numeric) return; if (itDecimalLength <= 0) return; // 小数模式判断删除小数点后,各位数是否超出 int itIndex = this.Text.IndexOf("."); if (e.KeyCode == Keys.Delete && itIndex >= this.SelectionStart & itIndex <= this.SelectionStart + this.SelectionLength) { if (this.SelectionLength <= 0 && this.Text.Length - 1 > itIntegerLength) { e.Handled = true; } else if (this.SelectionLength > 0 && this.Text.Length - this.SelectionLength > itIntegerLength) { e.Handled = true; } } } /// <summary> /// 押金文本框KeyPress事件 /// </summary> /// <param name="sender">押金文本框</param> /// <param name="e">按键KeyPress事件数据</param> /// <res></res> private void TextBoxEx_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { // 如果为正常模式则退出检查 if (enumInputType != InputTypeEnum.Numeric) return; // 如果为整数模式 if (itDecimalLength <= 0) { if (!char.IsDigit(e.KeyChar)) { ///// 如果不是数字,且不是退格键,则退出 if (e.KeyChar != char.Parse("8")) e.Handled = true; } else { ///// 如果是数字,且长度超出,则退出 if (this.Text.Length >= itIntegerLength & this.SelectionLength < 1) e.Handled = true; } return; } // 如果为小数模式 if (e.KeyChar == char.Parse(".")) { ///// 如果小数点输入 if (this.Text.IndexOf(".") > 0) // 如是小数点,并且已经存在,则放弃输入 e.Handled = true; else if (this.SelectionStart <= 0) // 如是小数点,且在起点输入,则放弃输入 e.Handled = true; else if (this.SelectionStart > itIntegerLength) // 如是小数点,且整数长度大于7,则放弃输入 e.Handled = true; else { // 如是小数点,且整数长度大于7,则放弃输入 int integerLength = this.Text.Substring(0, this.SelectionStart).Length; int decimalLength = this.Text.Substring(this.SelectionStart).Length; if (integerLength > itIntegerLength || decimalLength > itDecimalLength) e.Handled = true; } } else if (char.IsDigit(e.KeyChar)) { ///// 如果是整数输入 int index = this.Text.IndexOf("."); if (index < 0 && this.Text.Length >= itIntegerLength) // 如果不存在小数点,且长度大于5,则放弃输入 e.Handled = true; else if (index > 0) { // 如果存在小数点,则向下判断 int integerLength = this.Text.Substring(0, index).Length; int decimalLength = this.Text.Substring(index + 1).Length; if (integerLength > itIntegerLength || decimalLength > itDecimalLength) // 如果整数长度>5或小数长度>2,,则放弃输入 e.Handled = true; else if (integerLength == itIntegerLength && this.SelectionStart <= itIntegerLength) { if (this.SelectionLength < 1) // 如果整数长度=5,且输入在整数范围内,则退出 e.Handled = true; else if (this.SelectionStart <= index & this.SelectionStart + this.SelectionLength > index) { if (this.Text.Length - this.SelectionLength >= itIntegerLength) e.Handled = true; } } else if (decimalLength == itDecimalLength && this.SelectionStart > index & this.SelectionLength < 1) { // 如果小数长度=2,且输入在整数范围内,则退出 e.Handled = true; } } } else if (e.KeyChar == char.Parse("8")) { ///// 如果是退格键输入 int index = this.Text.IndexOf("."); // 如果是退格键,且删除后小数点后,整数长度大于5,则退出 if (index + 1 == this.SelectionStart && this.Text.Length > (itIntegerLength + 1)) e.Handled = true; } else { ///// 以上三者都不是,退出 e.Handled = true; } } #endregion 肯定是哪个老师说你们不懂的,可以去csdn问,结果那帮学生再也不做作业了。 <input onchange="value=(value.match(/\d+/g)||['']).join(',')" /><input type="text" name="a" onblur="validate(this.value)" /> function validate(str) { var re = /^\d+(?=\,{0,1}\d+$|$)/; if (!re.test(str)){ alert("输入错误"); } } 为什么我TabPage的Click事件中的函数没有被执行? 高手们,快来啊!高分求救dataGrid焦点问题?? 面向对象编程的迷惑,欢迎大家来讨论 新年散分之一 在CS程序中如何取得动态checkBox的值? 求助高手!!!!问题难办!! 将YUYV422视频图像数据转化为RBG显示,结果图像就这样了。。。 那里有programming c#下载? 非UI线程访问UI线程里的控件但不更新它需要invoke吗 C#串口数据接收问题 C# 及时通信软件 如何做到及时读取消息 2.0 关于Timer的问题
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
{
e.Handled = true;
}
}
2.可以用正则 例如2楼
{
if (e.KeyChar == 0x20) e.KeyChar = (char)0; //禁止空格键
if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return; //处理负数
if (e.KeyChar > 0x20)
{
try
{
double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
}
catch
{
e.KeyChar = (char)0; //处理非法字符
}
}
}private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
或者private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar!='\b')//这是允许输入退格键
{
if((e.KeyChar<'0')||(e.KeyChar>'9'))//这是允许输入0-9数字
{
e.Handled = true;
}
}
}private void button1_Click(object sender, EventArgs e)
{
string text = this.textBox1.Text;
if (text != null)
MessageBox.Show(text);
}private void textBox1_Validating(object sender, CancelEventArgs e)
{
const string pattern = @"^\d+\.?\d+$";
string content = ((TextBox)sender).Text;if (!(Regex.IsMatch(content, pattern)))
{
errorProvider1.SetError((Control)sender, "只能输入数字!");
e.Cancel = true;
}
else
errorProvider1.SetError((Control)sender, null);
}private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar=='.' && this.textBox1.Text.IndexOf(".")!=-1)
{
e.Handled=true;
}if(!((e.KeyChar>=48 && e.KeyChar<=57) || e.KeyChar=='.' || e.KeyChar==8))
{
e.Handled=true;
}}private void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true;//消除不合适字符
}
else if (Char.IsPunctuation(e.KeyChar))
{
if (e.KeyChar != '.' || this.textBox1.Text.Length == 0)//小数点
{
e.Handled = true;
}
if (textBox1.Text.LastIndexOf('.') != -1)
{
e.Handled = true;
}
}
}利用ASCII码处理办法、
{ if ((e.KeyChar <= 48 || e.KeyChar >=57) && (e.KeyChar != 8) && (e.KeyChar != 46))
e.Handled = true;
================48代表0,57代表9,8代表空格,46代表小数点
}
你可以参考一下。不能光用Keypress,比如退格键时不执行keypress,有时在按下退格键时,要验证删除后数据格式是否正确。
#region " 控制文本框类型输入"
/// <summary>
/// 押金文本框KeyDown事件
/// </summary>
/// <param name="sender">押金文本框</param>
/// <param name="e">按键KeyDown事件数据</param>
/// <res></res>
private void TextBoxEx_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// 如果不是小数模式,退出
if (enumInputType != InputTypeEnum.Numeric) return;
if (itDecimalLength <= 0) return; // 小数模式判断删除小数点后,各位数是否超出
int itIndex = this.Text.IndexOf(".");
if (e.KeyCode == Keys.Delete && itIndex >= this.SelectionStart & itIndex <= this.SelectionStart + this.SelectionLength)
{
if (this.SelectionLength <= 0 && this.Text.Length - 1 > itIntegerLength)
{
e.Handled = true;
}
else if (this.SelectionLength > 0 && this.Text.Length - this.SelectionLength > itIntegerLength)
{
e.Handled = true;
}
}
} /// <summary>
/// 押金文本框KeyPress事件
/// </summary>
/// <param name="sender">押金文本框</param>
/// <param name="e">按键KeyPress事件数据</param>
/// <res></res>
private void TextBoxEx_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// 如果为正常模式则退出检查
if (enumInputType != InputTypeEnum.Numeric) return; // 如果为整数模式
if (itDecimalLength <= 0)
{
if (!char.IsDigit(e.KeyChar))
{
///// 如果不是数字,且不是退格键,则退出
if (e.KeyChar != char.Parse("8"))
e.Handled = true;
}
else
{
///// 如果是数字,且长度超出,则退出
if (this.Text.Length >= itIntegerLength & this.SelectionLength < 1)
e.Handled = true;
} return;
} // 如果为小数模式
if (e.KeyChar == char.Parse("."))
{
///// 如果小数点输入
if (this.Text.IndexOf(".") > 0)
// 如是小数点,并且已经存在,则放弃输入
e.Handled = true;
else if (this.SelectionStart <= 0)
// 如是小数点,且在起点输入,则放弃输入
e.Handled = true;
else if (this.SelectionStart > itIntegerLength)
// 如是小数点,且整数长度大于7,则放弃输入
e.Handled = true;
else
{
// 如是小数点,且整数长度大于7,则放弃输入
int integerLength = this.Text.Substring(0, this.SelectionStart).Length;
int decimalLength = this.Text.Substring(this.SelectionStart).Length;
if (integerLength > itIntegerLength || decimalLength > itDecimalLength)
e.Handled = true;
}
}
else if (char.IsDigit(e.KeyChar))
{
///// 如果是整数输入
int index = this.Text.IndexOf(".");
if (index < 0 && this.Text.Length >= itIntegerLength)
// 如果不存在小数点,且长度大于5,则放弃输入
e.Handled = true;
else if (index > 0)
{
// 如果存在小数点,则向下判断
int integerLength = this.Text.Substring(0, index).Length;
int decimalLength = this.Text.Substring(index + 1).Length;
if (integerLength > itIntegerLength || decimalLength > itDecimalLength)
// 如果整数长度>5或小数长度>2,,则放弃输入
e.Handled = true;
else if (integerLength == itIntegerLength && this.SelectionStart <= itIntegerLength)
{
if (this.SelectionLength < 1)
// 如果整数长度=5,且输入在整数范围内,则退出
e.Handled = true;
else if (this.SelectionStart <= index & this.SelectionStart + this.SelectionLength > index)
{
if (this.Text.Length - this.SelectionLength >= itIntegerLength)
e.Handled = true;
}
}
else if (decimalLength == itDecimalLength && this.SelectionStart > index & this.SelectionLength < 1)
{
// 如果小数长度=2,且输入在整数范围内,则退出
e.Handled = true;
} }
}
else if (e.KeyChar == char.Parse("8"))
{
///// 如果是退格键输入
int index = this.Text.IndexOf(".");
// 如果是退格键,且删除后小数点后,整数长度大于5,则退出
if (index + 1 == this.SelectionStart && this.Text.Length > (itIntegerLength + 1))
e.Handled = true;
}
else
{
///// 以上三者都不是,退出
e.Handled = true;
} }
#endregion
<input type="text" name="a" onblur="validate(this.value)" /> function validate(str)
{
var re = /^\d+(?=\,{0,1}\d+$|$)/;
if (!re.test(str)){
alert("输入错误");
}
}