title: Regex validation results
body: <p>In a web form, I have a TextBox object with a RegularExpressionValidator. My problem is that when the textBox is empty, the validator returns that my string(an empty string) is valid, but when I want to check this again using a Regex object from code with the same expression, the result is that the empty string did not matches with the expression. Why does this happen?</p>
  
  <p>Here is my code:</p>
  
  <p>ASPX:</p>
  
  <pre><code>&lt;table style="background-color:#eeeeee; padding:10"&gt;
            &lt;tr valign="top"&gt;
               &lt;td colspan="3"&gt;
                  &lt;asp:Label ID="lblOutput" 
                       Text="Enter a 5-digit ZIP Code" 
                       runat="server"
                       AssociatedControlID="TextBox1"/&gt;
               &lt;/td&gt;
            &lt;/tr&gt;
  
            &lt;tr&gt;
               &lt;td colspan="3"&gt;
                  &lt;b&gt;Personal Information&lt;/b&gt;
               &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
               &lt;td align="right"&gt;
                  Zip Code:
               &lt;/td&gt;
               &lt;td&gt;
                  &lt;asp:TextBox id="TextBox1" 
                       runat="server"/&gt;
               &lt;/td&gt;
               &lt;td&gt;
                  &lt;asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                       ControlToValidate="TextBox1"
                       ValidationExpression="^\d\d*$"
                       Display="Static"
                       ErrorMessage="ZIP code must be 5 numeric digits"
                       EnableClientScript="False" 
                       runat="server"/&gt;
               &lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
               &lt;td&gt;&lt;/td&gt;
               &lt;td&gt;
                  &lt;asp:Button ID="Button1" text="Validate" 
                       OnClick="ValidateBtn_Click" 
                       runat="server" /&gt;
               &lt;/td&gt;
               &lt;td&gt;&lt;/td&gt;
            &lt;/tr&gt;
         &lt;/table&gt;
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code>protected void ValidateBtn_Click(Object sender, EventArgs e)
          {
  
  
              RegularExpressionValidator minValidator = (RegularExpressionValidator)FindControl("RegularExpressionValidator1");
              minValidator.Validate();
              if (!minValidator.IsValid)
              {
                  lblOutput.Text = "Page is InValid.";
              }
              else
              {
                  lblOutput.Text = "Page is Valid.";
              }
              String exp = @"^\d\d*$";
              Regex r = new Regex(exp);
              if (r.IsMatch(TextBox1.Text))
              {
                  Response.Write("strings matches");
              }
              else
              {
                  Response.Write("strings does not matches");
              }
  
          }
  </code></pre>
  
  <p>And here is a screenshot with the page:</p>
  
  <p><img src="https://i.stack.imgur.com/8GrxH.png" alt="enter image description here"></p>