正则表达式只允许介于100和999999之间的数字

siv*_*iva 1 c# regex compact-framework

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

谢谢,Lui.

Ser*_*ier 9

你不需要正则表达式.

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

编辑:如果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
}
Run Code Online (Sandbox Code Playgroud)

  • @mob:我是用户意识的.当我被要求验证用户输入时,我不会拒绝0100.用户会回复_Stupid @#$ computer_.她是对的! (2认同)