C#文本框限制按键输入

xiaoxiao2021-07-05  225

C#文本框限制按键输入

对照ASCII码表

1.仅支持输入数字
private void txt_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) { e.Handled = true; //禁止输入 } }
2.仅支持输入数字和字母
private void txt_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 &&!Char.IsLetterOrDigit(e.KeyChar)) { e.Handled = true; } }
3.输入年龄 《120岁
private void txtAge_KeyPress(object sender, KeyPressEventArgs e) { if (Char.IsDigit(e.KeyChar)) { e.Handled = (Convert.ToInt16(this.txtAge.Text + e.KeyChar) >120); } else if(e.KeyChar == 8) { e.Handled = false; } else { e.Handled = true; } }
4.判断是否是邮箱格式
public bool IsEmail(string value) { return System.Text.RegularExpressions.Regex.IsMatch(value, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); }
转载请注明原文地址: https://www.6miu.com/read-4821356.html

最新回复(0)