C# 正则表达式限制输入

清疚 2023-10-18 19:30 213阅读 0赞

Winform 中:

建立一个类,方便以后使用,内容如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.Text.RegularExpressions;
  8. namespace UI
  9. {
  10. public class TextSet
  11. {
  12. /// <summary>
  13. /// 限制只可输入数字
  14. /// </summary>
  15. /// <param name="e"></param>
  16. public static void setNumber(KeyPressEventArgs e)
  17. {
  18. if (e.KeyChar != '\b' && !Char.IsDigit(e.KeyChar))
  19. {
  20. e.Handled = true;
  21. MessageBox.Show("请输入数字!");
  22. return;
  23. }
  24. }
  25. /// <summary>
  26. /// 限制只能输入汉字或字母
  27. /// </summary>
  28. /// <param name="c"></param>
  29. public static void setChinese(KeyPressEventArgs e)
  30. {
  31. Regex rg = new Regex(@"^[\u4e00-\u9fa5a-zA-Z\b]+$"); //\b是退格键
  32. if (!rg.IsMatch(e.KeyChar.ToString()))
  33. {
  34. e.Handled = true;
  35. MessageBox.Show("只能输入汉字或字母!");
  36. return;
  37. }
  38. }
  39. /// <summary>
  40. /// 只能输入字母或数字
  41. /// </summary>
  42. /// <param name="e"></param>
  43. public static void setEnglishOrNum(KeyPressEventArgs e)
  44. {
  45. if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')
  46. || (e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
  47. {
  48. e.Handled = false;
  49. }
  50. else
  51. {
  52. e.Handled = true;
  53. MessageBox.Show("密码只能是字母或者数字!");
  54. return;
  55. }
  56. }
  57. }
  58. }

针对 Textbox 中的事件:

  1. private void txtUserID_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3. TextSet.setNumber(e);
  4. }

WPF 中:

建立一个类,方便以后使用,内容如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows.Input;
  8. namespace UI
  9. {
  10. public class Limit
  11. {
  12. /// <summary>
  13. /// 限制只能输入数字
  14. /// </summary>
  15. /// <param name="e"></param>
  16. public void limitnumber(TextCompositionEventArgs e)
  17. {
  18. Regex re = new Regex("[^0-9]+");
  19. e.Handled = re.IsMatch(e.Text);
  20. }
  21. /// <summary>
  22. /// 限制只能输入数字1到7
  23. /// </summary>
  24. /// <param name="e"></param>
  25. public bool limitnumberOneToServen(TextCompositionEventArgs e)
  26. {
  27. Regex re = new Regex("[^1-7]+");
  28. e.Handled = re.IsMatch(e.Text);
  29. return e.Handled;
  30. }
  31. /// <summary>
  32. /// 限制只能输入数字和小数点
  33. /// </summary>
  34. /// <param name="e"></param>
  35. public void limitnumber1(TextCompositionEventArgs e)
  36. {
  37. Regex re = new Regex("[^0-9.]+");
  38. e.Handled = re.IsMatch(e.Text);
  39. }
  40. }
  41. }

针对 Textbox 中的事件:

  1. private void TxtBoxID_PreviewTextInput(object sender, TextCompositionEventArgs e)
  2. {
  3. Limit limit = new Limit();
  4. limit.limitnumber(e);
  5. }

代码按需更改

推荐阅读

Regex Class (System.Text.RegularExpressions)

发表评论

表情:
评论列表 (有 0 条评论,213人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C#表达式

    没用到多少正则的判断,有点懒,闲着写的。主要写java的时候会具体,C\不想弄了。 1、画窗体: ![Image 1][] 2、实现判断类,其中写入错误日志,暂时可

    相关 C++表达式

    正则表达式(regular expression)是计算机科学中的一个概念,又称规则表达式,通常简写为regex、regexp、RE、regexps、regexes、regex