Regular Expression to check Posting or Channel names

A couple of weeks ago someone asked the question on the public newsgroup on how to verify a given posting name using regular expressions.

Here is a solution for this problem:

public bool CheckName(string name)
    // test for other characters as the ones in the list
    Regex regEx1 = new Regex(@”[^A-Z0-9 _\-().]”, RegexOptions.IgnoreCase);

    // test for double ‘.’
    Regex regEx2 = new Regex(@”\.{2}”, RegexOptions.None);

    // test for ‘.’ at the end
    Regex regEx3 = new Regex(@”[\.]$”, RegexOptions.None);    

    string Name = txtName.Text.Trim();

    Match match1 = regEx1.Match(Name);
    Match match2 = regEx2.Match(Name);
    Match match3 = regEx3.Match(Name);

    // valid = no invalid chars, no double dot, no dot at the end
    bool valid = !match1.Success && !match2.Success && !match3.Success; 

    return valid;

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.