简体   繁体   English

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

[英]Regex to allow only numbers between 100 and 999999

Can anyone help with C# code using regular expressions to validate a textbox which accepts only numbers between 100 and 999999 任何人都可以使用正则表达式帮助C#代码来验证仅接受100到999999之间的数字的文本框

Thanks, Lui. 谢谢,吕

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() . 编辑:如果CF是目标,那么您不能使用int.TryParse() Fallback on int.Parse() instead and type a little more error-catching code: 改用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
}

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. 我不记得默认情况下C#是否锚定RE,所以我也将它们放入了。

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

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 如果要允许前导零(但仍保留6位数的限制),则正则表达式为

^(?=[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. 最后一个可能需要一些解释:首先使用正向先行[ (?=) ]确保整个输入为3到6位数字,然后确保它由任意数量的前导零组成,后跟一个范围在100-999999之间的数字。

However , it's possibly a good idea to use something more suited to the task (maybe a numeric comparison?). 但是 ,最好使用更适合该任务的内容(也许是一个数字比较?)。

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
}

这将达到目的:

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

您可以考虑的另一种方法

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

Why not use a NumericUpDown control instead which lets you specifiy a Minimum and Maximum value? 为什么不使用NumericUpDown控件代替它来指定最小值和最大值? 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);
}

也许接受前导零:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM