正则表达式仅允许 100 到 999999 之间的数字

发布于 2024-12-01 11:33:40 字数 72 浏览 0 评论 0原文

任何人都可以帮助使用正则表达式的 C# 代码来验证仅接受 100 到 999999 之间数字的文本框

谢谢, 吕.

Can anyone help with C# code using regular expressions to validate a textbox which accepts only numbers between 100 and 999999

Thanks,
Lui.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

白芷 2024-12-08 11:33:40

您不需要为此使用正则表达式。

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
}

You don't need a regex for this.

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

EDIT: If the CF is targetted, then you can't use int.TryParse(). Fallback on int.Parse() instead and type a little more error-catching code:

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
}
格子衫的從容 2024-12-08 11:33:40

您的要求转换为三到六位数字,首先不是零。我不记得 C# 默认情况下是否锚定 RE,所以我也将它们放入其中。

^[1-9][0-9]{2,5}$

Your requirement translates to three to six digits, first not zero. I can't remember whether C# anchors REs by default, so I've put them in too.

^[1-9][0-9]{2,5}$
﹏雨一样淡蓝的深情 2024-12-08 11:33:40

一种简单的方法是使用正则表达式

^[1-9][0-9]{2,5}$

如果您想允许前导零(但仍保持 6 位数字限制),则正则表达式将是

^(?=[0-9]{3,6}$)0*[1-9][0-9]{2,5}

最后一个可能值得一些解释:它首先使用正向先行 [(?= )] 以确保整个输入为 3 到 6 位数字,然后确保它由任意数量的前导零后跟 100-999999 范围内的数字组成。

但是,使用更适合该任务的东西可能是个好主意(也许是数字比较?)。

A straightforward approach would be to use the regex

^[1-9][0-9]{2,5}$

If you want to allow leading zeroes (but still keep the 6-digit limit) the regex would be

^(?=[0-9]{3,6}$)0*[1-9][0-9]{2,5}

This last one probably merits some explanation: it first uses positive lookahead [(?=)] to make sure that the whole input is 3 to 6 digits, and then it makes sure that it's made up of any number of leading zeroes followed by a number in the range 100-999999.

However, it's possibly a good idea to use something more suited to the task (maybe a numeric comparison?).

‘画卷フ 2024-12-08 11:33:40

这就能解决问题:

^[1-9]\d{2,5}$

This will do the trick:

^[1-9]\d{2,5}$
美胚控场 2024-12-08 11:33:40

你必须使用正则表达式吗?怎么样

int result;
if(Int.TryParse(string, out result) && result > 100 && result < 999999) {
    //do whatever with result
}
else
{
    //invalid input
}

Do you have to use regex? How about

int result;
if(Int.TryParse(string, out result) && result > 100 && result < 999999) {
    //do whatever with result
}
else
{
    //invalid input
}
猥琐帝 2024-12-08 11:33:40

您可以考虑的另一种方法

[1-9]\d{2,5}

another approach you could consider

[1-9]\d{2,5}

对你再特殊 2024-12-08 11:33:40

为什么不使用 NumericUpDown 控件来代替您指定了最小值和最大值吗?
而且它也只允许数字,从而节省了额外的验证以确保可以输入任何非数字的内容

从示例中:

public void InstantiateMyNumericUpDown()
{
   // Create and initialize a NumericUpDown control.
   numericUpDown1 = new NumericUpDown();

   // Dock the control to the top of the form.
   numericUpDown1.Dock = System.Windows.Forms.DockStyle.Top;

   // Set the Minimum, Maximum, and initial Value.
   numericUpDown1.Value = 100;
   numericUpDown1.Maximum = 999999;
   numericUpDown1.Minimum = 100;

   // Add the NumericUpDown to the Form.
   Controls.Add(numericUpDown1);
}

Why not use a NumericUpDown control instead which lets you specifiy a Minimum and Maximum value?
And it will only allow numbers too, saving you additional validation to ensure anything non-numeric can be entered

From the example:

public void InstantiateMyNumericUpDown()
{
   // Create and initialize a NumericUpDown control.
   numericUpDown1 = new NumericUpDown();

   // Dock the control to the top of the form.
   numericUpDown1.Dock = System.Windows.Forms.DockStyle.Top;

   // Set the Minimum, Maximum, and initial Value.
   numericUpDown1.Value = 100;
   numericUpDown1.Maximum = 999999;
   numericUpDown1.Minimum = 100;

   // Add the NumericUpDown to the Form.
   Controls.Add(numericUpDown1);
}
飘逸的'云 2024-12-08 11:33:40

也许接受前导零:

^0*[1-9]\d{2,5}$

And maybe accepting leading zeros:

^0*[1-9]\d{2,5}$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文