Validation function code review Any suggestions on cleaning up this code:

 public override bool Validate(Control control, object value)
    {
      bool res = false;
      string str = string.Empty;

      try
      {
        if (((string)value).Length &gt; 0)
          str = value.ToString();
      }
      catch (Exception e)
      {
        res = false;
        this.ErrorText = "Bad values " + e.Message;
        this.ErrorType = ErrorType.Critical;
      }

      //Is it empty?
      if (String.IsNullOrEmpty(str))
      {
        res = false;
        this.ErrorType = ErrorType.Critical;
      }

      Regex RegNrShortRegexDarm = new Regex(@"([F][A][D][-][Z]\d{3})");
      Regex RegNrLongRegexDarm = new Regex(@"([F][A][D][-][Z]\d{3}[-][0-9]+$)");
      Regex RegNrShortRegexBrust = new Regex(@"([F][A][B][-][Z]\d{3})");
      Regex RegNrLongRegexBrust = new Regex(@"([F][A][B][-][Z]\d{3}[-][0-9]+$)");

      if (selectedItem == Constants.Item1)
      {
        this.ErrorText = "The correct form is: FAD-Zxxx oder FAD-Zxxx-x !";

        //has the form FAD-Zxxx
        if (RegNrShortRegexDarm.IsMatch(str) &amp;&amp; str.Length == 8)
        {
          // Number is valid
          res = true;
        }

        //has the form FAD-Zxxx-x
        if (RegNrLongRegexDarm.IsMatch(str) &amp;&amp; str.Length &gt;= 10 &amp;&amp; str.Length &lt;= 12)
        {
          // Number is valid
          res = true;
        }
      }
      else if (selectedItem == Constants.Item2)
      {
        this.ErrorText = "The correct form is: FAB-Zxxx oder FAB-Zxxx-x !";

        //has the form FAB-Zxxx
        if (RegNrShortRegexBrust.IsMatch(str) &amp;&amp; str.Length == 8)
        {
          // Number is valid
          res = true;
        }

        //has the form FAB-Zxxx-x
        if (RegNrLongRegexBrust.IsMatch(str) &amp;&amp; str.Length &gt;= 10 &amp;&amp; str.Length &lt;= 12)
        {
          // Number is valid
          res = true;
        }
      }

      return res;
    }


Later Edit:

I've reached the following solution:

public override bool Validate(Control control, object value)
{
  var str = value as string;
  var letter = (selectedItem == Constants.Item1) ? "D" : "B";
  var errText = String.Format("The correct form is: FA{0}-Zxxx or FA{0}-Zxxx-x !", letter);

  if (string.IsNullOrWhiteSpace(str))
  {
    this.ErrorType = ErrorType.Critical;
    this.ErrorText = errText;
    return false;
  }

  string regExString = String.Format(@"FA{0}-Z\d{{3}}", letter);
  Regex shortRegEx = new Regex(regExString);
  Regex longRegEx = new Regex(regExString + @"-\d+$");

  bool isMatch = ((shortRegEx.IsMatch(str) &amp;&amp; IsValidShortLength(str)) || (longRegEx.IsMatch(str) &amp;&amp; IsValidLongLength(str)));

  if (!isMatch)
  {
    this.ErrorText = errText;
  }
  return isMatch;

}

private static bool IsValidShortLength(string str)
{
  return str.Length == 8;
}

private static bool IsValidLongLength(string str)
{
  return (str.Length &gt;= 10 &amp;&amp; str.Length &lt;= 12);
}
The first check in the try block is not needed at all. It just checks if the value is string, and it's unlikely to be something else. If this validation is attached to a control, who's value is not a string, then this is an error, and I would throw an exception "Bad values", so the code is corrected.

Then, if the value is null or empty, you have an error, why would you inspect all the other possibilities?

Also - where you use the control itself? Why you pass it, if you do not use it?

Anyway, if I was to check the string for valid formatting, I would do this:

        var str = value as String;

        var letter = (selectedItem == Constants.Item1) ? "D" : "B";
        var errText = String.Format("The correct form is: FA{0}-Zxxx oder FA{0}-Zxxx-x !", letter);

        if (str == null 
            || str.Length &lt; 8 
            || !(str.Length &gt;= 10 &amp;&amp; str.Length &lt;=12))
        {
            this.ErrorType = ErrorType.Critical;
            this.ErrorText = errText;
            return false;
        }

        var regExString = @"([F][A][" + letter + @"][-][Z]\d{3})";
        Regex shortRegEx = new Regex(regExString);
        Regex longRegEx = new Regex(regExString + @"[-][0-9]+$)");

        var isMatch = (shortRegEx.IsMatch(str) || longRegEx.IsMatch(str));
        if (!isMatch)
        {
            this.ErrorText = errText;
        }

        return isMatch;