c# - 正则表达式只允许 100 到 999999 之间的数字

标签 c# regex compact-framework

谁能帮助 C# 代码使用正则表达式来验证只接受 100 到 999999 之间数字的文本框

谢谢, 吕。

最佳答案

你不需要正则表达式。

int n;
if (!int.TryParse(textBox.Text.Trim(), out n) || n<100 || n>999999)
{
  // Display error message: Out of range or not a number
}

编辑:如果以 CF 为目标,则不能使用 int.TryParse()。转而使用 int.Parse() 并键入更多错误捕获代码:

int n;
try
{
  int n = int.Parse(textBox.Text.Trim());
  if (n<100 || n>999999)
  {
    // Display error message: Out of range
  }
  else
  {
    // OK
  }
}
catch(Exception ex)
{
   // Display error message: Not a number. 
   //   You may want to catch the individual exception types 
   //   for more info about the error
}

关于c# - 正则表达式只允许 100 到 999999 之间的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7178894/

相关文章:

c# - Linq 过滤历史记录中的行差异

c# - 字符串连接 Visual Studio 2019 上的 int.ToString() 错误

javascript - 在javascript正则表达式之后将输入光标设置到适当的位置

c# - .NET CF 3.5 中 System.Threading.EventWaitHandle..ctor 的 ApplicationException

c# - 如何在 COmpact Framework 中将 ControlBox 设置为 false 时保留 ControlBox UI 感觉(win mobile)

linq-to-sql - Compact Framework 中的 LINQ To SQL

c# - 配置在 Windows 身份验证站点中托管的带有匿名访问的 Web 服务

c# - 防止键盘打开时页面屏幕向上滚动(C#-XAML-Windows Store App)

regex - 每个正则表达式解析和替换嵌套匹配

java - java中使用正则表达式进行模式匹配