/// <summary> /// 只能输入数字和空格 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void Number(object sender, TextCompositionEventArgs e) { Regex re = new Regex("[^0-9]+"); e.Handled = re.IsMatch(e.Text); return; } //卡号只能输入数字 public void NumberCardID(object sender, TextCompositionEventArgs e) { Regex re = new Regex("[^0-9.-]+"); e.Handled = re.IsMatch(e.Text); return; } /// <summary> /// 禁止输入数字和空格 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void NoNumber(object sender, TextCompositionEventArgs e) { Regex re = new Regex("^[0-9]*"); e.Handled = re.IsMatch(e.Text); return; } /// <summary> /// 手机号正则表达式 /// </summary> /// <param name="phoneNumber"></param> public void Phone(string phoneNumber) { //电信手机号正则 string dianxin = @"^1[3578][01379]\d{8}$"; Regex dReg = new Regex(dianxin); //联通手机号正则 string liantong = @"^1[34578][01256]\d{8}$"; Regex tReg = new Regex(liantong); //移动手机号正则 string yidong = @"^(134[012345678]\d{7}|1[34578][012356789]\d{8})$"; Regex yReg = new Regex(yidong); if (dReg.IsMatch(phoneNumber) == false && tReg.IsMatch(phoneNumber) == false && yReg.IsMatch(phoneNumber) == false) throw new Exception("手机号码格式不正确,请重新输入"); //抛出提示信息 } /// <summary> /// 禁用所有中英文特殊字符(***中文字符微软拼音输入法下有点问题***) /// </summary> /// <param name="textBox"></param> public static void ForbidSpechars(System.Windows.Controls.TextBox textBox) { if (Regex.IsMatch(textBox.Text.Trim(), @"((?=[\x21-\x7e]+)[^A-Za-z0-9])") || Regex.IsMatch(textBox.Text.Trim(), @"[\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b| \uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010| \u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5|\u00b7]")) { textBox.Text = textBox.Text.Substring(0, textBox.Text.Length - 1); textBox.SelectionStart = textBox.Text.Length; } } /// <summary> /// 只能输入中文和字母 /// </summary> /// <param name="keyPressE">KeyPressEventArgs内容</param> public static void InputHanEng(KeyPressEventArgs keyPressE) { Regex rg = new Regex("^[\u4e00-\u9fa5]$"); //正则表达式只输入中文 if (!rg.IsMatch(keyPressE.KeyChar.ToString()) && keyPressE.KeyChar != '\b') { keyPressE.Handled = true; } if (keyPressE.KeyChar >= 'a' && keyPressE.KeyChar <= 'z' || keyPressE.KeyChar >= 'A' && keyPressE.KeyChar <= 'Z') { keyPressE.Handled = false; } } /// <summary> /// 只能输入中文汉字 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void InputChinese(object sender, TextCompositionEventArgs e) { Regex re = new Regex("^[\u4e00 - \u9fa5]{ 0, }$"); if (e.Handled = re.IsMatch(e.Text)) { e.Handled = false; } else { e.Handled = true; } } public static void PhoneNumber(string phone) { //电信手机号码正则 string dianxin = @"^1[3578][01379]\d{8}$"; Regex dReg = new Regex(dianxin); //联通手机号正则 string liantong = @"^1[34578][01256]\d{8}$"; Regex tReg = new Regex(liantong); //移动手机号正则 string yidong = @"^(134[012345678]\d{7}|1[34578][012356789]\d{8})$"; Regex yReg = new Regex(yidong); if (dReg.IsMatch(phone) || tReg.IsMatch(phone) || yReg.IsMatch(phone)) { } else { throw new Exception("手机号格式不正确,请重新输入"); } } /// <summary> /// 只能输入数字和退格 /// </summary> /// <param name="keyPressE">KeyPressEventArgs内容</param> public static void InputNumBs(KeyPressEventArgs keyPressE)//TODO命名修改,位置修改 { if (keyPressE.KeyChar > '9' || keyPressE.KeyChar < '0' && keyPressE.KeyChar != (char)8) { keyPressE.Handled = true; } return; } /// <summary> /// 只能输入英文字母和数字 /// </summary> /// <param name="keyPressE">KeyPressEventArgs内容</param> public static void InputNumEng(object sender, TextCompositionEventArgs e) { Regex re = new Regex("^[A-Za-z0-9]+$"); if (e.Handled = re.IsMatch(e.Text)) { e.Handled = false; } else { e.Handled = true; } } /// <summary> /// 禁止输入特殊字符 /// </summary> /// <param name="textBox"></param> public static void ForbidSpechars(System.Windows.Controls.TextBox textBox) { if (Regex.IsMatch(textBox.Text.Trim(), @"((?=[\x21-\x7e]+)[^A-Za-z0-9])") || Regex.IsMatch(textBox.Text.Trim(), @"[\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b| \uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010| \u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5|\u00b7]")) { textBox.Text = textBox.Text.Substring(0, textBox.Text.Length - 1); textBox.SelectionStart = textBox.Text.Length; } } /// <summary> /// 清空grid下的所有日期控件的文本 /// </summary> /// <param name="grid"></param> public static void ClearDate(Grid grid) { foreach (UIElement element in grid.Children) { if (element is DatePicker) { (element as DatePicker).Text = ""; } } } /// <summary> /// 禁止文本框复制粘贴 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void Prohibit(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = false; e.Handled = true; } #region 密码框小眼睛事件 /// <summary> /// 鼠标抬起事件 /// </summary> /// <param name="pwd"></param> /// <param name="txt"></param> public void ButtonUp(PasswordBox pwd, TextBox txt) { pwd.IsEnabled = true; pwd.Visibility = Visibility.Visible; txt.IsEnabled = false; txt.Visibility = Visibility.Hidden; } /// <summary> /// 鼠标按下事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void ButtonDown(PasswordBox pwd, TextBox txt) { txt.Text = pwd.Password; txt.IsEnabled = true; txt.Visibility = Visibility.Visible; pwd.IsEnabled = false; pwd.Visibility = Visibility.Hidden; } #endregion //禁止输入空格 public void SpaceKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Space) { e.Handled = true; } }
有些方法使用的时候要禁掉输入法
InputMethod.IsInputMethodEnabled=“False”